array problems

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
hyper_st8
Forum Newbie
Posts: 20
Joined: Mon Aug 18, 2003 5:27 am
Contact:

array problems

Post 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
Image
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Could we see a bit of the code?

Mac
User avatar
hyper_st8
Forum Newbie
Posts: 20
Joined: Mon Aug 18, 2003 5:27 am
Contact:

Post 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>';

?>
User avatar
irealms
Forum Contributor
Posts: 215
Joined: Mon Apr 28, 2003 7:10 am
Location: Leeds

Post 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.
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post 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
User avatar
hyper_st8
Forum Newbie
Posts: 20
Joined: Mon Aug 18, 2003 5:27 am
Contact:

Post 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?
Post Reply