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
hyper_st8
Forum Newbie
Posts: 20 Joined: Mon Aug 18, 2003 5:27 am
Contact:
Post
by hyper_st8 » Mon Jan 12, 2004 5:48 pm
Hello!
When I try the following script, only the value in the last field gets ticked:
Code: Select all
<?
$nameID = "2";
$db = mysql_connect("localhost", "", "") or die("unable to connect to the database");
mysql_select_db("test", $db) or die("unable to select the database");
$sqlA = "SELECT * FROM Anames";
$objSQLA = mysql_query ($sqlA) or die ("Query 1 Failed: ".mysql_error());
$sqlB= "SELECT Enames FROM Bnames WHERE namesID ='$nameID'";
$objSQLB = mysql_query ($sqlB) or die ("Query 2 Failed: ".mysql_error());
$rowB = mysql_fetch_assoc($objSQLB);
while ($names = mysql_fetch_array($objSQLA)) {
echo $names["names"];
echo "<input type="checkbox" name="name"";
if($names["names"] == $rowB["Enames"]) {
echo "checked";
}
echo ">";
}
?>
any ideas?! Thank you in advance!
Straterra
Forum Regular
Posts: 527 Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:
Post
by Straterra » Mon Jan 12, 2004 6:25 pm
I think A doesn't work because you are selecting every cell..I thought you could only retrieve one row, then use the fetch array function to tell what collumn in that row you wanted...
hyper_st8
Forum Newbie
Posts: 20 Joined: Mon Aug 18, 2003 5:27 am
Contact:
Post
by hyper_st8 » Tue Jan 13, 2004 3:05 am
The idea is to have a list of all names and the ones that are already associated with that ID is checked... Can't understand why it doesn't work because it went from working to just selecting the last value in the field...
Bill H
DevNet Resident
Posts: 1136 Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:
Post
by Bill H » Tue Jan 13, 2004 8:53 am
You have a bunch of checkboxes all with the same name.
If you do that you need to make them an array:
Code: Select all
<?php
echo "<input type="checkbox" name="name[]"";
?>
infolock
DevNet Resident
Posts: 1708 Joined: Wed Sep 25, 2002 7:47 pm
Post
by infolock » Tue Jan 13, 2004 10:43 am
Try this and see if it works... It's untested as i'm at school right now, but i think it might.
Code: Select all
<?
$nameID = "2";
//******** ADDED ARRAYS *********
$Bnames = array();
$ANames = array();
$chkboxname = array();
//###############################
mysql_connect("localhost", "", "") or die("unable to connect to the database");
mysql_select_db("test", $db) or die("unable to select the database");
$sqlA = "SELECT * FROM Anames";
$objSQLA = mysql_query ($sqlA) or die ("Query 1 Failed: ".mysql_error());
while ($names = mysql_fetch_array($objSQLA))
{
$Anames[] = $names['name'];
}
$sqlB= "SELECT Enames FROM Bnames WHERE namesID ='".$nameID."'";
$objSQLB = mysql_query ($sqlB) or die ("Query 2 Failed: ".mysql_error());
while ($rowB = mysql_fetch_assoc($objSQLB))
{
$Bnames[] = $rowB['Enames']; //you had $rowB['name'] before.. Shouldn't this be Ename (since that's what you used in your query)??
}
for($i=0; $i<count($Anames); $i++)
{
$chkboxname[$i] = 'name'.$i;
echo $Anames[$i];
echo '<input type="checkbox" name="'.$chkboxname[$i].'"';
if($Anames[$i] == $Bnames[$i])
{
echo 'checked';
}
echo ">"; // wtf is this for?
}
?>
hyper_st8
Forum Newbie
Posts: 20 Joined: Mon Aug 18, 2003 5:27 am
Contact:
Post
by hyper_st8 » Wed Jan 14, 2004 4:09 am
Yeah good point Bill H! I needed to make it into an array.
Thanks infolock! That works great! as for that the last echo, it was used to close the check input, which is nice!