Page 1 of 1
array problems
Posted: Mon Aug 18, 2003 5:27 am
by hyper_st8
I'm using a while loop to draw out all of the usernames from a database but when you try and insert more than one username it only inserts the first username into the database, and since the number of usernames is never fixed, that is it depends upon how many users there are within that user(s) group it is hard to have a fixed number within an array
and the array is dependant upon how many user(s) are ticked

Posted: Mon Aug 18, 2003 5:31 am
by twigletmac
Could we see a bit of the code?
Mac
Posted: Mon Aug 18, 2003 5:36 am
by hyper_st8
Sure!
the code for the check boxes is the following while loop:
Code: Select all
<?php
$pquery = "SELECT username from users WHERE groupname='$_SESSION[groupname]'";
$presult = mysql_query($pquery, $db_conn);
echo '<table><tr align="left" valign="top"><td class="main"><b>Job User(s) are for:</b><br />';
while ($prow = mysql_fetch_array($presult)){
echo '<input type="checkbox" name="username" value="'.$prow['username'].'">'.$prow['username'].'<br />';
}
echo '</td></tr></table>';
?>
Posted: Mon Aug 18, 2003 5:37 am
by irealms
hyper_st8 is working on same project as me, i've seen the code. He needs to know how to create an array dependant on the amount of users ticked.
So if 1 user is ticked the array has 1 entry, and if 3 were ticked it would create an array with 3 entries.
Posted: Mon Aug 18, 2003 6:55 am
by Coco
how about naming the selects as an array?
ie:
Code: Select all
<?php
$pquery = "SELECT username from users WHERE groupname='$_SESSION[groupname]'";
$presult = mysql_query($pquery, $db_conn);
echo '<table><tr align="left" valign="top"><td class="main"><b>Job User(s) are for:</b><br />';
$x=0;
while ($prow = mysql_fetch_array($presult)){
echo '<input type="checkbox" name="username['.$x.']" value="'.$prow['username'].'">'.$prow['username'].'<br />';
$x++;
}
echo '</td></tr></table>';
?>
i use this occasionally, you will end up with the array $_POST[username][]... you are only getting one result from the form since you have used the same name for several form fields, and only one will be used
Posted: Tue Aug 19, 2003 4:24 am
by hyper_st8
thanks, I am new to arrays and understand that it creates a different variable for each username but how would you create the array that puts the $_POST into the database?