Jump to content

looping problem


alan6566

Recommended Posts

I am trying to get it to display the table where only some rows will be shown. It works on picking up each indivual value from the othere page but when it comes to displaying the table it only pics up the first value and only displays that.

 

Here is my code bellow

 

<? 
session_start();

if ($_SESSION['userName'])

{}

else
{
header('location:../index.php');
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Deliver-Pizza Order</title>
<link href="css/order.css"  rel="stylesheet" type="text/css" />

</head>

<body>

<div id="container">

     <div id="header">
    <img src="images/logo.jpg" alt="logo" />
        
         <a href="log-off.php" id="logg-off" >log off</a>
        
    <ul id="nav">
    <li><a href="home.php" target="_self">Home</a></li> <span> | </span>
    <li><a href="Pizza-Topping.php" target="_self">Pizza Topping</a></li> <span> | </span>
    <li><a href="order.php" target="_self">Order</a></li> <span> | </span>
        <li><a href="Account.php" target="_self">Account Details</a></li> <span> | </span> 
       </ul> 
</div>
   
<div id="Featured">
<img src="images/banner1.jpg" alt="banner" name="banner"" ID="banner"></div><!--end Featured-->
<div class="featuredimage" id="main">
<div id="content" class="tiles">
    <h1>Order</h1><hr />

<div id ='staff'><div style="width:920px; height:autopx; overflow:auto; heightmax:300px">

<table>
<tr>
<th>pizza number</th>
<th>Type</th>
<th>Size</th>
<th>Topping</th>
    <th>Cost</th>
<th>Information</th>
    </tr>

    <?php

mysql_connect("localhost", "root", "")or die("cannot connect"); 
    mysql_select_db("deliverpizza")or die("cannot select DB");

$sql="SELECT * FROM `pizzatopping` ";

$result= mysql_query($sql);	


while($row = mysql_fetch_assoc($result))
{
    	$pizza    = $row['id'];
	if(isset($_POST["pizza$pizza"])) {
    		

		if ($_POST["pizza$pizza"] == $row['id']){

		$id = $row['id'];

		$dis="SELECT * FROM `pizzatopping` where id='$id'";
		$result= mysql_query($dis);	

		while($row =mysql_fetch_assoc($result)){
			?>
            <tr>
		<td><?php echo $row['id'] ?></td>
		<td><?php echo $row['type'] ?></td>
		<td><?php echo $row['size'] ?></td>
		<td><?php echo $row['topping'] ?></td>
		<td><?php echo $row['cost'] ?></td>
		<td><?php echo $row['info'] ?></td>
            </tr>
            <?php
		}

		}
	}
} 

?>
</table>
</div>
</div>
    
</div>
</div>
<!--end content-->

<div id="footer"><span><p>Created By Ryan Williams</p></span></div><!--end footer-->
</div><!--end container-->




</body>
</html>

Link to comment
Share on other sites

First, what is this?

if ($_SESSION['userName'])

{}

else
{
header('location:../index.php');
}

 

Just use this:

if (!isset($_SESSION['userName']))
{
    header('location:../index.php');
}

 

Second, don't use PHP short tags.

 

As to your problem. The only thing that sticks out at me is this

if($_POST["pizza$pizza"] == $row['id'])

 

There is a condition to check if the record from that table match a POST value. I would *assume* there would only be one record from that table with that matching id? But, I see you do have a subsequent query to get the same data using the id from the first query. That makes no sense. If you only need the data for particular IDs, then put that in the WHERE clause of the query instead of doing that logic in the PHP code. I'm assuming that these are checkboxes. If so, you should do this differently to make it easier. I would also suggest putting all your PHP logic at the top of the page - easier to maintain.

 

So, in your form, change the fields from what I think is something like this

<input type="checkbox" name="pizza12" value="1" />

where 12 is the id of the item being selected

 

Change to this

<input type="checkbox" name="pizzas[]" value="12" />

 

You will now have $_POST['pizzas'] returned as an array with the values of all the selected items

 

Then, I would change your current code to this

<?php

session_start();

if(!isset($_SESSION['userName']))
{
    header('location:../index.php');
}

//Get the array of selected pizzas (force to be intvals and remove empty values)
$selectedPizzasAry = array_map('intval', $_POST['pizzas']);
//Remove empty values
$selectedPizzasAry = array_filter($selectedPizzasAry);

$output = ''; //Var to hold the output

//Check that there were valid values
if(!count($selectedPizzas))
{
    $output .= "<tr><td colspan='6'>No pizzas were selected.</td></tr>\n";
}
else
{
    mysql_connect("localhost", "root", "")or die("cannot connect"); 
    mysql_select_db("deliverpizza")or die("cannot select DB");

    //Create and run query to get selected records
    $selectedPizzasStr = implode(', ', $selectedPizzasAry);
    $query = "SELECT `id`, `type`, `size`, `topping`, `cost`, `info`
              FROM `pizzatopping`
              WHERE id IN ({$selectedPizzasStr})";
    $result = mysql_query($query);

    //Process the results
    if(!$result)
    {
        $output  = "<tr><td colspan='6'>There was a problem getting the records.</td></tr>\n";
        //This line for debugging only
        $output .= "<br>Query:<br>{$query}<br>Error:<br>" . mysql_error();
    }
    elseif(!mysql_num_rows$result())
    {
        $output .= "<tr><td colspan='6'>There was no matching records.</td></tr>\n";
    }
    else
    {
        while($row = mysql_fetch_assoc($result))
        {
            $output .= "<tr>\n";
            $output .= "    <td>{$row['id']}</td>\n";
            $output .= "    <td>{$row['type']}</td>\n";
            $output .= "    <td>{$row['size'}</td>\n";
            $output .= "    <td>{$row['topping']}</td>\n";
            $output .= "    <td>{$row['cost']}</td>\n";
            $output .= "    <td>{$row['info']}</td>\n";
            $output .= "</tr>\n";
        }
    }
} 

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Deliver-Pizza Order</title>
<link href="css/order.css"  rel="stylesheet" type="text/css" />

</head>

<body>

<div id="container">
    <div id="header">
        <img src="images/logo.jpg" alt="logo" />
        <a href="log-off.php" id="logg-off" >log off</a>
        <ul id="nav">
            <li><a href="home.php" target="_self">Home</a></li> <span> | </span>
            <li><a href="Pizza-Topping.php" target="_self">Pizza Topping</a></li> <span> | </span>
            <li><a href="order.php" target="_self">Order</a></li> <span> | </span>
            <li><a href="Account.php" target="_self">Account Details</a></li> <span> | </span> 
       </ul> 
    </div>

    <div id="Featured"><img src="images/banner1.jpg" alt="banner" name="banner"" ID="banner"></div><!--end Featured-->
    <div class="featuredimage" id="main">
        <div id="content" class="tiles">
        <h1>Order</h1><hr />
            <div id ='staff'>
                <div style="width:920px; height:autopx; overflow:auto; heightmax:300px">
                    <table>
                        <tr>
                            <th>pizza number</th>
                            <th>Type</th>
                            <th>Size</th>
                            <th>Topping</th>
                            <th>Cost</th>
                            <th>Information</th>
                        </tr>
                        <?php echo $output; ?>
                    </table>
                </div>
            </div>
        </div>
    </div>
    <!--end content-->
    <div id="footer"><span><p>Created By Ryan Williams</p></span></div><!--end footer-->
</div><!--end container-->

</body>
</html>

Link to comment
Share on other sites

The page that sends the information to the page is bellow. If I get it just to display the value it works but when it displays i get it to diplay from the data base it does not work

 

<? 
session_start();

if ($_SESSION['userName'])

{}

else
{
header('location:../index.php');
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Deliver-Pizza Topping</title>
<link href="css/pizza-topping.css"  rel="stylesheet" type="text/css" />

</head>

<body>

<div id="container">

     <div id="header">
    <img src="images/logo.jpg" alt="logo" />
        
        <a href="log-off.php" id="logg-off" >log off</a>
        
    <ul id="nav">
    <li><a href="home.php" target="_self">Home</a></li> <span> | </span>
    <li><a href="Pizza-Topping.php" target="_self">Pizza Topping</a></li> <span> | </span>
    <li><a href="order.php" target="_self">Order</a></li> <span> | </span>
        <li><a href="Account.php" target="_self">Account Details</a></li> <span> | </span> 
       </ul> 
</div>
   
<div id="Featured">
<img src="images/banner1.jpg" alt="banner" name="banner"" ID="banner"></div><!--end Featured-->
<div class="featuredimage" id="main">
<div id="content" class="tiles">
    <h1>Pizza-Topping</h1><hr />
    <p>The list bello are the pizzas available to order</p>
    
    <div id ="staff"><div style="width:920px; height:autopx; overflow:auto; heightmax:300px">

<left> <table>
<tr>
<th>pizza number</th>
<th>Type</th>
<th>Size</th>
<th>Topping</th>
<th>Cost</th>
<th>Information</th>
<th> <form name="pizza-order" action="order.php" method="post"><input type="submit" name="submit" Value="Order"></th>
</tr>
<tr>

<?php


mysql_connect("localhost", "root", "")or die("cannot connect"); 
    mysql_select_db("deliverpizza")or die("cannot select DB");

$sql="SELECT * FROM `pizzatopping` ";
$result= mysql_query($sql);	

while($row =mysql_fetch_assoc($result)) {


?>
<td><?php echo $row['id'] ?></td>
<td><?php echo $row['type'] ?></td>
<td><?php echo $row['size'] ?></td>
<td><?php echo $row['topping'] ?></td>
<td><?php echo $row['cost'] ?></td>
<td><?php echo $row['info'] ?></td>
<td> 
<input type="checkbox" Name="pizza<?php echo $row['id'] ?>" value="<?php echo $row['id']   ?>"  />
</td> 


</tr></left>
<?php

}
?>

</table>
</div>
<!--<form name="pizza-order" action="order.php">
Pizza Number<input type="text" Name="pizza-1">Quanity<input type="text" Name="quanit-1y"><br/>
Order 2<input type="text" name="order-2"><br/>
<input type="submit" name="submit" Value="Order">
</form>-->

</div>
    
</div>
</div>
<!--end content-->

<div id="footer"><span><p>Created By Ryan Williams</p></span></div><!--end footer-->
</div><!--end container-->




</body>
</html>

Link to comment
Share on other sites

If you are not going to take any of the advice that is given, why are you asking for help?

 

The way that you are currently creating your checkboxes is flawed and making the whole process much more difficult than it should be. You should not be giving all the of the checkboxes unique names. You will already know which checkboxes are checked based upon the passed values. Name all the checkboxes the same (as an array).

Link to comment
Share on other sites

i am taking the advice. but it is not seeming to work. i just though that you may want to see the page where it is getiing sent from.

 

and if you were going to design and ordering system how would you do it? as i have been stuck for days of this one thing.

Link to comment
Share on other sites

The code you last posted has the same problem I advised you to fix. You are naming the checkboxes as "pizzaID", where "ID" is the ID number of that value. That is NOT needed and actually makes the problem more difficult to solve. You also didn't solve the illogical if/else at the start of the page that is the same as the other one I pointed out.

 

Here is how I would code this page. Note none of this is tested as I don't have your database. I expect you to find and fix any minor errors.

 

<? 
session_start();

if (!isset($_SESSION['userName']))
{
    header('location:../index.php');
}


mysql_connect("localhost", "root", "")or die("cannot connect"); 
    mysql_select_db("deliverpizza")or die("cannot select DB");

$sql="SELECT * FROM `pizzatopping`";
$result= mysql_query($sql) or die(mysql_error());    

$orderForm = '';
while($row =mysql_fetch_assoc($result))
{
    $orderForm .= "<tr>\n";
    $orderForm .= "<td>{$row['id']}</td>\n";
    $orderForm .= "<td>{$row['type']}</td>\n";
    $orderForm .= "<td>{$row['size']}</td>\n";
    $orderForm .= "<td>{$row['topping']}</td>\n";
    $orderForm .= "<td>{$row['cost']}</td>\n";
    $orderForm .= "<td>{$row['info']}</td>\n";
    $orderForm .= "<td><input type='checkbox' name='pizzas[]' value='{$row['id']}' /></td>\n";
    $orderForm .= "</tr>\n";
}
?>

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Deliver-Pizza Topping</title>
    <link href="css/pizza-topping.css"  rel="stylesheet" type="text/css" />
</head>

<body>

<div id="container">
    <div id="header">
        <img src="images/logo.jpg" alt="logo" />
        <a href="log-off.php" id="logg-off" >log off</a>
            
        <ul id="nav">
            <li><a href="home.php" target="_self">Home</a></li> <span> | </span>
            <li><a href="Pizza-Topping.php" target="_self">Pizza Topping</a></li> <span> | </span>
            <li><a href="order.php" target="_self">Order</a></li> <span> | </span>
            <li><a href="Account.php" target="_self">Account Details</a></li> <span> | </span> 
        </ul> 
    </div>
   
    <div id="Featured">
        <img src="images/banner1.jpg" alt="banner" name="banner"" ID="banner"></div><!--end Featured-->
        <div class="featuredimage" id="main">
            <div id="content" class="tiles">
                <h1>Pizza-Topping</h1><hr />
                <p>The list bello are the pizzas available to order</p>
        
                <div id ="staff"><div style="width:920px; height:autopx; overflow:auto; heightmax:300px">

                    <table>
                        <tr>
                            <th>pizza number</th>
                            <th>Type</th>
                            <th>Size</th>
                            <th>Topping</th>
                            <th>Cost</th>
                            <th>Information</th>
                            <th><form name="pizza-order" action="order.php" method="post"><input type="submit" name="submit" Value="Order"></th>
                        </tr>
                        <?php echo $orderForm; ?>
                    </table>
                </div>
                <!--<form name="pizza-order" action="order.php">
                Pizza Number<input type="text" Name="pizza-1">Quanity<input type="text" Name="quanit-1y"><br/>
                Order 2<input type="text" name="order-2"><br/>
                <input type="submit" name="submit" Value="Order">
                </form>-->

            </div>
        
        </div>
    </div>
    <!--end content-->
    <div id="footer"><span><p>Created By Ryan Williams</p></span></div><!--end footer-->
</div><!--end container-->

</body>
</html>

Link to comment
Share on other sites

You take the input submitted by the user and:

 

1. Validate that the values are appropriate (e.g. was a topping of wingnuts or anything which you do not provide).

2. Sanitize the values so that they are safe for use in a query (some validation can do this as well, such as verifying the amount is a valid number).

3. Create an insert query. Since you are wanting to insert multiple records you should "add up" all the records to build one insert query such as

INSERT INTO table_name
    (field1, field2, filed3)

VALUES
    ('record1-value1', 'record1-value2', 'record1-value3'),
    ('record2-value1', 'record2-value2', 'record2-value3'),
    ('record3-value1', 'record3-value2', 'record3-value3')

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.