Array Problem

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
jamrop
Forum Commoner
Posts: 80
Joined: Fri May 16, 2003 5:38 pm

Array Problem

Post by jamrop »

Hey

I am having a problem with an array.

I have a restaurant advert that has 5 listing of it food that it offers.
THe database has 2 tables. One for the advert and one for the food menu
so e.g.

Advert table:

Advert_id = 233

Menu table

Menu_id 1, Advert_id = 233, item= chips, Price=1.00
Menu_id 2, Advert_id = 233, item=fish, Price 2.00

Adding into these tables work fine. THe problem I am having if i want to edit the food menu. I use input type to display the food items, so i can edit them, but when i click on "sumbit" button (change menu), it just updates the last record, but does not update the rest.

Hard to explain, code is below.

Code: Select all

<?php
<?php

ob_start(); 
//start the session
session_start();
header("Cache-control: private"); //IE 6 Fix 
 

//check to make sure the session variable is registered
if(session_is_registered('username')){

//the session variable is registered, the user is allowed to see anything that follows



}
else{

//the session variable isn't registered, send them back to the login page
header( "Location: ../../login/login.php" );
}

//get database info
require('../../../dbinfo.php');

//extra checking system 
$checkuser="select member_id,member_type from members where username = '$username'";

$ch=mysql_query($checkuser,$db) or die ("Getting member info failed" . mysql_error());

while ($c = mysql_fetch_array($ch))

								{

								$id=$c["member_id"];

								$member_type=$c["member_type"];

								}

								if ($id==$check && $member_type =="restaurant") {



//get advert info
$sql="select member_id,username from members where username = '$username'";
$rs=mysql_query($sql,$db);
while ($r = mysql_fetch_array($rs))
								{
								$member_id=$r["member_id"];	
									
									  	$advert="select advert_id from advert where member_id = '$member_id' and advert_id = '$ad' 
										 order by date desc";
									    $ad=mysql_query($advert,$db)or die ("select failed" . mysql_error());
										while ($a=mysql_fetch_array($ad))
										{
										$advert_id = $a['advert_id'];
										}
										}
										

//If the user clicks on the sumbit button check for the following
										if (isset($_POST['submit'])){

								$valid=true;

								if ( !item  | !$price    )
					{$errmsg.="You have a blank field.<br>"; $valid=false;}}
	
//if everything is filled in then update the database		
if ($valid!=true)



					{echo ( $errmsg.$form);} else {
					$item=ucfirst($item);
					

					$updatead="update menu set item = '$item' 
					, price = '$price' 
					where menu_id = '$menu_id'";

					mysql_query($updatead,$db) or die ("Select Failed ");
					if (mysql_affected_rows()==1){
					
					
					header("location: ../restaurant_advert.php?check=$member_id"); }
					else 
					{echo "You did not change any settings";}
					

					}

					?>
<table width="60%" border="1" cellpadding="0" cellspacing="0" bordercolor="#FFFFFF">
                <tr bgcolor="#9999FF"> 
                  <td width="79%"><strong>Menu</strong></td>
                  <td width="21%"><strong>Price</strong></td>
                </tr>
                
			   <?php 

//need to get the listing from the menu
			   $menu="select advert.advert_id, menu.menu_id,menu.advert_id, menu.item, menu.price from advert, menu
										where advert.advert_id = menu.advert_id and advert.advert_id='$advert_id'";
										$ms=mysql_query($menu,$db) or die ("select failed" . mysql_error());
										while ($m=mysql_fetch_array($ms)) { 
			   $item=$m["item"];
			   $price=$m["price"];
			   $menu_id=$m["menu_id"];
			   
			   ?>
			   <form name="menu" method="post" action="<?php $PHP_SELF ?>">
			    <tr> 
                  <td><input type="hidden" name="menu_id" value="<?php echo $menu_id ?>">
 				  <input type="text" name="item" value="<?php echo $item ; ?>" size="40" maxlength="59"></td>
				  
				  
                  <td>£<input type="text" name="price" value="<?php echo $price ; ?>" size="5"></td>
                </tr>
                <?php  } ?>
              </table>
			  <input type="submit" name="submit" value="Change list">
?>

Hope someone can help me

Many thanks
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

a submit-button only submits the form it's in.
But you have

Code: Select all

while ($m=mysql_fetch_array($ms)) &#123;
	...

<form name="menu" method="post" action="<?php $PHP_SELF ?>">
	...
	<input type="submit" name="submit" value="Change list">
multiple forms, one for each item
jamrop
Forum Commoner
Posts: 80
Joined: Fri May 16, 2003 5:38 pm

Post by jamrop »

hey

but don't i need the while loop to get all the menu listing? as if i put the
} before the form, it would only display the one item in the menu instead of the 5 listing

Many thanks
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Pseudo code just to demonstrate.

Code: Select all

<form start>
<?php
  while (loop) &#123;
    // do stuff as...
    echo '<input text>';
    echo '<input hidden>';
  &#125;
?>
<submit button>
<form end>
Point being, you should move the 'one timers' <form> and <submit> outside the while-loop. That makes them only print once.
Post Reply