[SOLVED] PHP variable handling problems

This is a discussion on "[SOLVED] PHP variable handling problems" within the PHP Forum section. This forum, and the thread "[SOLVED] PHP variable handling problems are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Oct 14th, 2007, 22:21
New Member
Join Date: Jul 2007
Location: Houston, TX
Age: 25
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Exclamation [SOLVED] PHP variable handling problems

Basically my database is setup like this:

sections -> id,sid,name
menu -> item,sid

the menu sid corresponds with the section id

The code below is supposed to pull the section id from the sections table and then for every section, it is supposed to pull the menu item that belongs in that section.

Section1 has 3 items
item1 from section1
item2 from section1
item3 from section1

Section2 has 1 item
item1 from section2
item2 from section2

Section3 has 4 items
item1 from section3
item2 from section3
item3 from section3
item4 from section3


The first loop works fine, it picks up the correct section ids.
The second loop to pick up the actual items in the section fails. It acts like it is looping through(ie. every section gets the # of items it actually has) But the data is from the 1st section only. ie, the 2nd and 3rd and 4th sections all contain the data from the 1st section.


It actually looks like this: (please note the section# from which it is pulling the item....different from above)

Section1 has 3 items
item1 from section1
item2 from section1
item3 from section1

Section2 has 1 item
item1 from section1
item2 from section1

Section3 has 4 items
item1 from section1
item2 from section1
item3 from section1





PHP: Select all

$sql "SELECT * FROM sections where rid='$rid' ORDER BY sid";

$content .= $sql."<br>";
$modextra "<hr><h1>Non-Sectioned</h1>";
$result mysql_query($sql) or die(mysql_error());
$nrows mysql_num_rows($result);
while(
$row mysql_fetch_array($result)) {
    
$secid[] = $row['id'];
    
$sname[] = $row['name'];
}
for(
$j 0$j $nrows$j++) {
$content .= "<hr><h1>$sname[$j]</h1>";
$content .= "<TABLE BORDER=1 cellpadding=5 cellspacing=0 align=center width=100%>\n";
$sql "SELECT * FROM menu where rid='$rid' AND sid='${$secid[$j]}' ORDER BY id";
$content .= $sql."<br>";
$result mysql_query($sql) or die(mysql_error());
$rows mysql_num_rows($result);
while(
$row mysql_fetch_array($result)) {
    
$id[] = $row['id'];
    
$iname[] = $row['iname'];
    
$idesc[] = $row['idesc'];
    
$iprice[] = $row['iprice'];
    
$iextra1[] = $row['extra1'];
    
$iextra2[] = $row['extra2'];
    
$isid[] = $row['sid'];

}
for(
$i 0$i $rows$i++) {
    
$content .= " <TR>\n";
    
$a $i;
            
$content .= "<TD valign=top width=200><font style=\"font-size:8pt;\"><b>ID:</b> "$id[$a] ."<br><b><a name=\""$id[$a] ."\">Name:</a></b> "$iname[$a]."<br><b>Price: </b>".$iprice[$a]."</font></td><td><font style=\"font-size:8pt;\"><b>Extra1: </b>".$iextra1[$a]."<br><b>Extra2: </b>".$iextra2[$a]."</font></td><td><b>Description:</b><br> "$idesc[$a] ."</td><td><input type=button value='Delete Item' onclick=\"document.location='menuitemdelete.php?i=confirm&rid=".$rid."&id=$id[$a]'\"><br><input type=button value='Modify Item' onclick=\"document.location='menu_itemmod.php?id=$id[$a]'\"></td>";
    
$content .= " </TR>\n";
}
$content .= "</TABLE>\n"

Last edited by Rakuli; Oct 15th, 2007 at 09:18. Reason: changed [code] to [php] tags for easier reading
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Oct 15th, 2007, 03:58
Highly Reputable Member
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP variable handling problems

I think your problem is here:
PHP: Select all

$sql "SELECT * FROM menu where rid='$rid' AND sid='${$secid[$j]}' ORDER BY id"
Shouldn't "rid" be "id?"
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Oct 15th, 2007, 06:17
New Member
Join Date: Jul 2007
Location: Houston, TX
Age: 25
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP variable handling problems

No. Sorry I forgot to put that in the database section. rid is the restaurant id. I'm pulling the items from that restaurant only where the sid is the section id and ordering them id.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Oct 15th, 2007, 07:44
Highly Reputable Member
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP variable handling problems

where does the main FOR loop end? There's a missing "}".
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Oct 15th, 2007, 09:47
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP variable handling problems

Hi there,

The loops don't appear to be necessary.

I changed your code a bit, this way you can tell for sure if it;s the query that is wrong because the loops are inside of loops.

PHP: Select all

<?php

sql 
"SELECT * FROM sections where rid='$rid' ORDER BY sid";
$content .= $sql."<br>";
$modextra "<hr><h1>Non-Sectioned</h1>";
$result mysql_query($sql) or die(mysql_error());
$nrows mysql_num_rows($result);
while(
$row mysql_fetch_array($result)) {

    
$content .= '<hr><h1>' $row['name'] . '</h1>
                <TABLE BORDER=1 cellpadding=5 cellspacing=0 align=center width=100%>' 
"\n";
                
    
$sql "SELECT * FROM menu where rid='$rid' AND sid='${$row['id']}' ORDER BY id"// sid='$ ?? is that right?
    
    
$content .= $sql '<br />';
    
    
$result2 mysql_query($sql) or die(mysql_error());
        
        while (
$row2 mysql_fetch_assoc($result2))
            
            
$content .= '<tr>
                            <td valign="top" width="200" style="font-size: 8pt"><strong>ID : </strong>' 
$row2['id'] . '<br />
                                                                                <strong><a name="' 
$row2['id'] . '">Name : </a></strong>' $row2['iname'] . '<br />
                                                                                <strong>Price : </strong> ' 
$row2['iprice'] . '
                            </td><td style="font-size: 8pt">                    <strong>Extra1 : </strong> ' 
$row2['extra1'] . '<br />
                                                                                <strong>Extra2 : </strong> ' 
$row2['extra2'] . '
                            </td><td style="font-size: 8pt">                    <strong>Description : </strong> ' 
$row2['idesc'] . '
                            </td><td style="font-size: 8pt">                    <input type="button" value="Delete Item" onclick="document.location=\'menuitemdelete.php?i=confirm&amp;rid=' 
$rid '&amp;id=' $row2['id'] . '" /><br />
                                                                                <input type="button" value="Modify Item" onclick="document.location=\'menu_itemmod.php?id=' 
$row2['id'] . '" />
                            </td>
                        </tr>'
;
                                                                                
    
}

$content .= '</table>';
?>
Let me know how it goes,

Cheers
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Oct 15th, 2007, 22:10
New Member
Join Date: Jul 2007
Location: Houston, TX
Age: 25
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP variable handling problems

Had to modify a few things to fit it in with the rest of the code, but it works great

I think I was just making it too complicated. Remember KISS

Thanks a lot.

Last edited by DerrickE; Oct 15th, 2007 at 22:12.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Extracting filename without extension from variable string. frankenscarf JavaScript Forum 3 Jan 11th, 2008 16:54
[SOLVED] Sending variable from one php file to another eon201 PHP Forum 2 Oct 31st, 2007 15:48
[SOLVED] ASP Session Variable Monie Classic ASP 7 Oct 10th, 2007 02:44
variable problems... tehrobot Flash & Multimedia Forum 4 Sep 13th, 2006 13:01
[SOLVED] Passing my variable, help please Anonymous User PHP Forum 0 Feb 12th, 2005 13:37


All times are GMT. The time now is 06:25.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42