Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    New Coder
    Join Date
    Sep 2011
    Posts
    78
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Checkbox inside table not working correctly

    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):

    Code:
    <?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:
    Code:
    <?
    
    	$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>

  • #2
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    To start, you need to update how you name your checkbox:
    PHP Code:
    <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:
    PHP Code:
    <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.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #3
    New Coder
    Join Date
    Sep 2011
    Posts
    78
    Thanks
    15
    Thanked 0 Times in 0 Posts
    @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

  • #4
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Technically, you could use something simple like:
    PHP Code:
    <td><?php echo ($row['rating'] == 0) ? 'No rating' $row['rating'?></td>
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,023
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    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:
    Code:
    $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
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Quote Originally Posted by rangana View Post
    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.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,023
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    New Coder
    Join Date
    Sep 2011
    Posts
    78
    Thanks
    15
    Thanked 0 Times in 0 Posts
    @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

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,023
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,023
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    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.

    Code:
    <?
    	$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:
    Code:
        $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
    Code:
        $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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,023
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    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-ch...-an-array.html
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #12
    New Coder
    Join Date
    Sep 2011
    Posts
    78
    Thanks
    15
    Thanked 0 Times in 0 Posts
    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:
    Code:
    <?
    
    	$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;
    ";

  • #13
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,023
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    No, that debug is *CORRECT*.

    $id *SHOULD* be an array.

    When you use
    Code:
        $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:
    Code:
    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
    Code:
        ... 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
    Code:
    ... WHERE x IN ( 3, 7, 12 )...
    is the same as doing
    Code:
       ... WHERE ( x = 3 OR x = 7 OR x = 12 ) ...
    and so allows you to get many records with one query.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #14
    New Coder
    Join Date
    Sep 2011
    Posts
    78
    Thanks
    15
    Thanked 0 Times in 0 Posts
    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'

  • #15
    New Coder
    Join Date
    Sep 2011
    Posts
    78
    Thanks
    15
    Thanked 0 Times in 0 Posts
    i did notice that if i changed
    Code:
    $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
    Code:
    $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 & ???


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •