Web Hosting Talk







View Full Version : PHP: Driving me crazy!


FW-Mike
06-24-2003, 03:29 AM
I've got a table in my db called addons which has rows added in an admin area. ATM it looks basically like:

id oid name price
1 2 Domain Name 14.99
2 2 5 GB Bandwidth 4.99

I want to be able to echo out all the rows from that table and have checkboxes so that the user can select which ones they want then I want to register the ones they checked in a session so I can call it up later. What I have now doesn't work, I don't care if your fix corrects mine or does it completely different. Any help appreciated!


if(ISSET($_POST["addsub"]))
{

$i = 0;

foreach($_POST['add'] as $key => $v)
{

$i = $i++;

$addon[$i] = $key;

session_register("addon[$i]");
}
print_r ($_SESSION);

}

$result = mysql_query("SELECT * FROM addons WHERE oid='$opid' ORDER BY id ASC") or die("addons ".mysql_error());

if(mysql_num_rows($result) > 0)
{

while ($add = mysql_fetch_object($result))
{
echo "<tr><td colspan='2'><input type='checkbox' name='add[$add->id]'>";
echo "$add->name ($".$add->price.")";
echo"</td></tr>";
}


Even if i check both boxes, print_r ($_SESSION); just shows
Array ( [addon[0]] => )

Thanks in advance

Rich2k
06-24-2003, 04:52 AM
Have you initialised the session?

session_start();

Additionally the php manual states that you shouldn't use the old session_register() function in combination with the new method of $_SESSION

FW-Mike
06-24-2003, 01:11 PM
Yes rich, i did session_start(); at the top of the page.

Also, I'l switch it to the new way of


For example: $_SESSION['var'] = 'ABC';


But it still won't work properly.

FW-Mike
06-25-2003, 02:53 AM
I've still not got this solved, i'd really appreciate some help guys!

ArgWebDev
06-25-2003, 08:37 AM
if(!session_is_registered("addon")){
session_register("addon");
}
if(ISSET($_POST["addsub"]))
{

$i = 0;

foreach($_POST['add'] as $key => $v)
{

$i = $i++;

$addon[$i] = $key;

}
print_r ($_SESSION);

}

$result = mysql_query("SELECT * FROM addons WHERE oid='$opid' ORDER BY id ASC") or die("addons ".mysql_error());

if(mysql_num_rows($result) > 0)
{

while ($add = mysql_fetch_object($result))
{
echo "<tr><td colspan='2'><input type='checkbox' name='add[$add->id]'>";
echo "$add->name ($".$add->price.")";
echo"</td></tr>";
}

I try with this code and work fine.
My code is like your code, but I don't register all the array's, I only register one variable.
(sorry for my english :( )

If you need more help: leo [at] nkstudios.net

Rich2k
06-25-2003, 09:44 AM
Ah I didn't spot that, register the entire array, not a specific part of the array.

If you only want that part then allocate it to another string

e.g. $string = $arry[$i];