...

View Full Version : Checkbox inside table not working correctly



VickP07
10-01-2011, 04:09 AM
Hey guys,
Okay so here is what i am working on. Right now I am displaying a page with data from a Database. The data is being displayed in a table. The table columns contain the recipe name | Prep Time | Total Time | Rating
Then in each row the recipe info is displayed. I have added checkboxes before each recipe name to allow the user to select one or multiple recipes and then after clicking a submit button display additional data for that recipe or recipes.

Right now if only one recipe is selected (the checkbox) and you hit the submit button another page opens to display the additional info. This is working GREAT!!!

The problem i am having is if the user selects multiple recipes (checkboxes) the next page only shows one recipe and not the others as well.

Here is my code where I display on the home page the data along with the (checkboxes) and submit button ( THE CHECKBOXES CAN BE FOUND INSIDE THE WHILE STATEMENT):




<?php
$db = mysql_connect( "localhost","root", "temp1234");
mysql_select_db( "Peters_restaurantDB");

$sql = "
SELECT id, name, preptime, totaltime, rating
FROM recipes;
";

#test if select statement works
$result = mysql_query( $sql );
$result or die("My query ($sql) failed." );

if (mysql_num_rows ($result ) == 0 )
{
$err = true;
}
?>

<html>

<head>
<title>3342 Recipe DB</title>
</head>

<body>
<h2>Recipes in the Database...</h2><hr />

<p></p>
<h3>*Click on a recipe name to view additional info on it. </h3>
<h3>***You can also sort the recipes based on Recipe Name, Prep Time, Total time, or Rating. </h3>
<p></p>
<p></p>

<!--The code below creates the table to display all the data from the DB. The headers of all the column contain a link
to this same php file. The link allows the user to sort based on Recipe name, preptime, total time, and rating
I used a switch statement to test for what the user clicks on to sort, and then i pass the sql query.
-->

<table border="2" width ="500">
<tr>
<th><a href="PetersRecipeDB.php?sort=recipe">Recipe Name</a></th>
<th><a href="PetersRecipeDB.php?sort=prep">Prep Time</a></th>
<th><a href="PetersRecipeDB.php?sort=total">Total Time</a></th>
<th><a href="PetersRecipeDB.php?sort=rating">Rating</a></th>
</tr>

<?
$sortswitch = $_GET['sort'];

switch ($sortswitch) {
case "recipe":
$sql = "SELECT id, name, preptime, totaltime, rating FROM recipes ORDER BY name";
break;
case "prep":
$sql = "SELECT id, name, preptime, totaltime, rating FROM recipes ORDER BY preptime";
break;
case "total":
$sql = "SELECT id, name, preptime, totaltime, rating FROM recipes ORDER BY totaltime";
break;
case "rating":
$sql = "SELECT id, name, preptime, totaltime, rating FROM recipes ORDER BY rating";
break;
default:
$sql = "SELECT id, name, preptime, totaltime, rating FROM recipes";
break;
}

#test if select statement works
$result = mysql_query( $sql );
$result or die("My query ($sql) failed." );

#While statement used to pass data from DB into tables -->

while( $row = mysql_fetch_array( $result ) ) { ?>

<form name="input" action="ShoppingList.php" method="get">

<tr>

<td><a href="ShowRecipe.php?id=<?=$row['id'] ?>"><input type="checkbox" name="r_name" value="<?=$row['id']?>" /><?=$row['name'] ?></a></td>
<td><?=$row['preptime']?> mins</td>
<td><?=$row['totaltime']?> mins</td>
<td><?=$row['rating']?></td>
</tr>
<? } ?>

</table>


<input type="submit" value="Submit" />
</form>
<p>If you click the "Submit" button, the shopping list needed for t".</p>



<hr />
<p>The current date and time is: </h4></p> <code> <?php print strftime('%a') ?> <?php print strftime('%b') ?> <?php print strftime('%e') ?> <?php print strftime('%r') ?> <?php print strftime('%G')?> </code>

</body>
</html>




Here is the second page where i display my additional info based on the what checkbox the user chose:

<?

$db = mysql_connect( "localhost","root", "temp1234");
mysql_select_db( "Peters_restaurantDB");

$id = $_GET['r_name'];

$sql = "
SELECT id, name, description, preptime, totaltime, rating
FROM recipes
WHERE id = $id;
";

$sql4 = "
SELECT recipe_ingredient.amount, units_of_measure.name AS U_Name, ingredients.name
FROM recipe_ingredient
LEFT JOIN ingredients ON recipe_ingredient.ingredient_id = ingredients.id
LEFT JOIN units_of_measure ON ingredients.unit_id = units_of_measure.id
WHERE recipe_ingredient.recipe_id = $id;
";

$result = mysql_query( $sql );
$result or die("My query ($sql) failed." );


$row = mysql_fetch_array( $result);

#test if select statement works
$result4 = mysql_query( $sql4 );
$result4 or die("My query ($sql4) failed." );

?>

<html>

<head>
<title>3342 Recipe DB</title>
</head>

<body>
<h2>Shopping List</h2><hr />
<img src="http://www.bestgraph.com/gifs/gastronomie/cuisiniers/cuisiniers-03.gif" alt="Cooking" width="250" height="280" />

<p></p>
<p></p>

<table border="2" width ="700">
<tr>
<th>Recipe Name</th>
<th>Desc.</th>
<th>Prep Time</th>
<th>Total Time</th>
<th>Rating</th>
</tr>

<!--display the data for the recipe that the user clicked on -->
<tr><td><?=$row['name']?></td>
<td><?=$row['description']?></td>
<td><?=$row['preptime']?></td>
<td><?=$row['totaltime']?></td>
<td><?=$row['rating']?></td>
</tr>
</table>



<h4>Ingredients you need to buy:</h4>
<ul>
<? while ($row4 = mysql_fetch_array($result4) ) {?>
<li><?=$row4['amount']?> <?=$row4['U_Name']?> <?=$row4['name']?></li>
<? } ?>
</ul>


<!--Now this following line of code will be used to return to previous page-->
<p> </p>
<form method="get" ACTION="PetersRecipeDB.php">
<input type="submit" value="Back">
</form>

<hr />

<p>The current date and time is: </h4></p> <code> <?php print strftime('%a') ?> <?php print strftime('%b') ?> <?php print strftime('%e') ?> <?php print strftime('%r') ?> <?php print strftime('%G')?> </code>

</body>
</html>

rangana
10-01-2011, 05:04 AM
To start, you need to update how you name your checkbox:


<input type="checkbox" name="r_name" value="<?=$row['id']?>" />


All of the checkboxes have the same name, which means only one (the last checkbox) of all that is checked (if there are multiple checked boxes) will be POSTed to the next page.

The solution for this is to suffix these names with [] which will be accessed by PHP script as an array:


<input type="checkbox" name="r_name[]" value="<?=$row['id']?>" />


You will somehow have to update how you insert your values in DB. You can first loop through all the values of $_GET['r_name[]'] and add "commas" to them before inserting in your DB. If you need to access the fields that was set, you can use PHP's explode, but I believe you could manage it from that point... the important part which you missed is just the naming of the checkbox. ;)

Hope that helps.

VickP07
10-01-2011, 06:20 AM
@Rangana:
Okay im still new to all this php and mysql stuff so please help me understand something.

i added to the names [] and understand that this is now an array. Then on the second page i need to $id = $_GET['r_name[]']; and then somehow find a way to pass the id for whatever recipe the user selected in the previous page so I can do the following sql query:
$sql = "
SELECT id, name, description, preptime, totaltime, rating
FROM recipes
WHERE id = $id;
";

You said I could access the data by using the following function based on what ever the user checked:
explode(separator,string,limit)
I am not exactly sure how to use this function though?

Thank you

rangana
10-01-2011, 11:44 PM
Technically, you could use something simple like:


<td><?php echo ($row['rating'] == 0) ? 'No rating' : $row['rating'] ?></td>

Old Pedant
10-02-2011, 01:27 AM
If you are using checkboxes, then the user *COULD* select *ALL* the recipes by checking every check box.

How do you want to handle that???

Maybe the easiest answer, if you only want to allow one recipe at a time to be chosen, is change to using radio buttons. *THEN* you only need a simple name (no [ ], no array) because only one *can* be sent from the browser to PHP.

But if you want to use checkboxes, then you could do this:


$sql = "
SELECT id, name, description, preptime, totaltime, rating
FROM recipes
WHERE id IN (" . implode( ",", $_GET("r_name[]") ) . ")
";


Rangana gave you the wrong function. explode() takes a string and produces an array. You want to take an array and create a string. implode() does that. I know it is kind of against the rules, but... read the docs:
http://www.php.net/manual/en/function.implode.php

rangana
10-02-2011, 03:47 AM
You can first loop through all the values of $_GET['r_name[]'] and add "commas" to them before inserting in your DB. If you need to access the fields that was set, you can use PHP's explode

I mean explode on fetching the data and I forgot to explicitly mention implode() in inserting the data.

Sorry if I confused you and Vick.

Old Pedant
10-02-2011, 05:16 AM
Okay, I guess I can see using explode. Actually, I do the equivalent using GetString with ASP code.

But with PHP, you'd have to get the data from the DB into an array first, *then* explode that into a set of checkboxes. Probably more complex than he's ready for.

VickP07
10-02-2011, 05:50 AM
@old Pedant:

Okay so I do know that option buttons would be much better to allow the user to only select one at a time, and a lot easier to do. But i want to allow the user to select multiple checkboxes and see the data for each one he/she checked.

I tried the sql query u gave me and after i select ("check") two recipes and hit submit the next page opens and shows me this error:

Fatal error: Function name must be a string in /var/www/3342/paguilary/test/ShoppingList.php on line 21

Again i copied the query exactly the same as u posted

Old Pedant
10-02-2011, 06:35 AM
And which is line 21?

Anyway, I don't use PHP, and since that *SEEMS* to be a PHP error, I'll let somebody else help.

But if you don't show your code, it's hard for people to guess where the error might be in space.

Old Pedant
10-02-2011, 06:53 AM
Since you claim in that other thread that you shouldn't have started that indeed the line with implode in it is line 21, let's try to figure out the bug.

It's called DEBUGGING, and you simply can't program without knowing how to debug. Period.



<?
$db = mysql_connect( "localhost","root", "temp1234");
mysql_select_db( "Peters_restaurantDB");

$id = $_GET['r_name[]'];
// next line may not work...dunno what PHP does when you echo an array
// if next line gives an error, yank it out
echo "DEBUG: value of id is " . $id . "<hr/>\n";

$idlist = implode( ",", $id );
echo "DEBUG: value of idlist is " . $idlist . "<hr/>\n";

$sql = "
SELECT id, name, description, preptime, totaltime, rating
FROM recipes
WHERE id IN ( $idlist )
";

echo "DEBUG: sql is " . $sql . "<hr/>\n";
...


But you know, I *THINK* that the error may be here:


$id = $_GET['r_name[]'];

As I said, I'm not a PHP person, but I believe that, in order to get an array, you must use

$id = $_GET['r_name'];

That is, omit the [] from the name. PHP sees the [] in the name within the GET data and creates the array.

Again, *NOT* a PHP person, so try it both ways.

Old Pedant
10-02-2011, 06:55 AM
Yeah, I was right. Even a cursory GOOGLE for "php get array of checkboxes" yields tons of answers.

Example:
http://www.kavoir.com/2009/01/php-checkbox-array-in-form-handling-multiple-checkbox-values-in-an-array.html

VickP07
10-02-2011, 07:39 AM
Okay sorry for posting in another thread

Okay so i see what is happening now with the use of the debug procedures. Now my code looks like this:


<?

$db = mysql_connect( "localhost","root", "temp1234");
mysql_select_db( "Peters_restaurantDB");

$id = $_GET['r_name'];
echo "DEBUG: value of id is " . $id . "<hr/>\n";

$idlist = implode( ",", $id );
echo "DEBUG: value of idlist is " . $idlist . "<hr/>\n";

$sql = "
SELECT id, name, description, preptime, totaltime, rating
FROM recipes
WHERE id IN ( $idlist )
";

echo "DEBUG: sql is " . $sql . "<hr/>\n";



When i check two of the recipes on the home page and click submit the following message displays:

DEBUG: value of id is Array

SO i am taking this as instead of passing the 'id' which is needed to display the info the $id variable is only getting 'Array'

I need to pass the 'id' number to get the data using the next sql query following the code i just pasted above

$sql4 = "
SELECT recipe_ingredient.amount, units_of_measure.name AS U_Name, ingredients.name
FROM recipe_ingredient
LEFT JOIN ingredients ON recipe_ingredient.ingredient_id = ingredients.id
LEFT JOIN units_of_measure ON ingredients.unit_id = units_of_measure.id
WHERE recipe_ingredient.recipe_id = $id;
";

Old Pedant
10-02-2011, 08:25 AM
No, that debug is *CORRECT*.

$id *SHOULD* be an array.

When you use

$id = $_GET['r_name'];
and the field in your <form> in HTML was named r_name[] then PHP *automatically* converts all the (possibly many) values of r_name[] into an array.

One thing I learned in googling: You could replace the first DEBUG line with this:


echo "DEBUG: value of id is " . print_r( $id, TRUE ) . "<hr/>\n";

If I understand the docs correctly, that will dump out the contents of the array.

********

Okay, you got the first DEBUG message correctly.

What happened to the second one? The one that should show you the contents of $idlist???

If you never go that far--if the implode line threw an error, then this is beyond me. But what version of PHP are you using??


**********


Also, your understanding of SQL is flawed.

If you want the user to be able to select more than one checkbox, then you *MUST* use the SQL syntax

... WHERE recipe_ingredients.recipe_id IN ( 33, 117, 1892 )

(The numbers there are just examples, and the list of numbers can be from 1 to thousands.)

The IN ( ) operator in SQL functions the same as an [b]OR[b].

Doing

... WHERE x IN ( 3, 7, 12 )...
is the same as doing


... WHERE ( x = 3 OR x = 7 OR x = 12 ) ...

and so allows you to get many records with one query.

VickP07
10-02-2011, 08:45 AM
okay using the new echo code it does show it is getting the right 'id' with whatever was checked in the previous page.

the idlist also is working showing that it is getting the 'id' of whatever is checked
and the sql query debug also works but then my next sql query wont work because :

SELECT recipe_ingredient.amount, units_of_measure.name AS U_Name, ingredients.name FROM recipe_ingredient LEFT JOIN ingredients ON recipe_ingredient.ingredient_id = ingredients.id LEFT JOIN units_of_measure ON ingredients.unit_id = units_of_measure.id WHERE recipe_ingredient.recipe_id = Array

it should be getting the contents of 'id' instead of 'array'

VickP07
10-02-2011, 09:00 AM
i did notice that if i changed


$sql4 = "
SELECT recipe_ingredient.amount, units_of_measure.name AS U_Name, ingredients.name
FROM recipe_ingredient
LEFT JOIN ingredients ON recipe_ingredient.ingredient_id = ingredients.id
LEFT JOIN units_of_measure ON ingredients.unit_id = units_of_measure.id
WHERE recipe_ingredient.recipe_id = $id;
";


to


$sql4 = "
SELECT recipe_ingredient.amount, units_of_measure.name AS U_Name, ingredients.name
FROM recipe_ingredient
LEFT JOIN ingredients ON recipe_ingredient.ingredient_id = ingredients.id
LEFT JOIN units_of_measure ON ingredients.unit_id = units_of_measure.id
WHERE recipe_ingredient.recipe_id = $idlist;
";


It does show the data for one recipe (checked) working perfectly

but if i try and check two
then the query won't execute properly because it shows:

SELECT recipe_ingredient.amount, units_of_measure.name AS U_Name, ingredients.name FROM recipe_ingredient LEFT JOIN ingredients ON recipe_ingredient.ingredient_id = ingredients.id LEFT JOIN units_of_measure ON ingredients.unit_id = units_of_measure.id WHERE recipe_ingredient.recipe_id = 2,3

it doesn't like the ','
i thought about changing the , to an & ???

Old Pedant
10-03-2011, 05:35 AM
You need to make the *SAME CHANGE* that you made to the other query!


$sql4 = "
SELECT recipe_ingredient.amount, units_of_measure.name AS U_Name, ingredients.name
FROM recipe_ingredient
LEFT JOIN ingredients ON recipe_ingredient.ingredient_id = ingredients.id
LEFT JOIN units_of_measure ON ingredients.unit_id = units_of_measure.id
WHERE recipe_ingredient.recipe_id IN ( $idlist )
";

And for *EXACTLY* the SAME reasons.

Old Pedant
10-03-2011, 05:38 AM
But now you have other problems. Because you will have to figure out how to coordinate the movement through two different sets of records such that you have the right records associated.

This could and should have been solved by using *ONE* query, only, and using a JOIN between the two tables.

I hope you will pardon me for saying that at this point it may be prudent for you to go back to only getting one recipe at a time, maybe by changing to using radio buttons instead of checkboxes. Learning how to use a JOIN and and and may be more than you really want to tackle at this time.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum