Problem looping through database. PHP

MuK107

Senior member
Dec 30, 2001
270
0
0
Im having problems looping through my database. Im useing fast template and i cant get it to loop through and add more than one item. here is what i have...

$menu = "SELECT * FROM menu_left ORDER BY menu_order";

if ($complete != 1)
$menu .= " LIMIT 20";

$menu_left = mysql_query($menu);
while ($menurow = mysql_fetch_array($menu_left)) {
if ($menurow['menu_show'] == 1) {
$menu_link = $menurow['menu_link'];
$menu_name = $menurow['menu_name'];
} else {
echo "";
};
}

$tpl->assign(array(
'menu_link' => $menu_link,
'menu_name' => $menu_name,
)
);

then i have in the template...

<a href="{menu_link}.php">{menu_name}</a>

can you help me with this one?
 

oniq

Banned
Feb 17, 2002
4,196
0
0
When you do a mysql_query you need to specify the database mysql_query($menu,$db);
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
My answer may not actually be right, but I was using "==" instead of "=" in your while loop.

This looks like a problem to me:

You have a while loop that evaluates $menurow as either true or false. $menurow is set at the beggining of each loop. However, $menurow is always set to the exact same thing (mysql_fetch_array($menu_left)). So, if mysql_fetch_array($menu_left) is false at the begginning of the loop, your loop will be skipped. If it's true at the beginning of the loop, you should get stuck in an infinite loop. I'm no PHP programmer, but I don't see anything PHP specific about this program.
 

MuK107

Senior member
Dec 30, 2001
270
0
0
im still not quite sure what im supposed to do to my code though. i put the '==' in there and nothing changed. not sure what else im supposed to do. i am connecting to the database and table in another section of the code - oniq.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: MuK107
i think so, is it saying while this is true then do this?

yes:

while ( stuff in parentheses is true)
{
do the stuff in brackets
}

Now you have to determine if the contents of the () in the while loop are true when the while loop starts.
 

MuK107

Senior member
Dec 30, 2001
270
0
0
well i had it working before i used a template. but after i tryed working with fasttemplate, templatepower, smarty, and unltrutemplate i havnt goten it to loop through untill its done.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Do you know how to write PHP at all, or are you just copying someone else's code? Do you have any idea how to go about finding out if the statement you're evaluating is true?
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Originally posted by: MuK107
well i had it working before i used a template. but after i tryed working with fasttemplate, templatepower, smarty, and unltrutemplate i havnt goten it to loop through untill its done.

templates have nothing to do with while loops.
 

kt

Diamond Member
Apr 1, 2000
6,015
1,321
136
Originally posted by: notfred
Do you know how to write PHP at all, or are you just copying someone else's code? Do you have any idea how to go about finding out if the statement you're evaluating is true?

Actually, there's nothing wrong with the statement:

while ($menurow = mysql_fetch_array($menu_left))

That's the correct syntax for this particular evaluation. In any other languages, it may not be correct but for PHP that is the correct syntax.

As for the problem at hand, I can tell from your code that you are only passing one variable to the template. You need to make a call to parse for each item you want to pass to the template. Basically, only at the very end do you pass what you have parsed so far to the template. Then the template is applied to all the items you have parsed so far. Not sure if I am making any sense here. But here's a pseudo code.

while ( there are still items in database ) {
$tpl->parse( your content or body )
}

$tpl->parse( your main page )
$tpl->fastprint( print out your page to browser)


Basically, you'll need a template for your body content and then a template for your whole page. The code you have right now will only print out the last item in your query.

Oh, and I would recommend writing a separate function to do the content parsing. It will make it easier to read and understand. It's late and I just got back from a party, so I will probably write a more coherent post once I get some sleep.
 

MuK107

Senior member
Dec 30, 2001
270
0
0
I did write all the code by my self. But im very new to php. here is what i got from your post kt....

$menu = "SELECT * FROM menu_left ORDER BY menu_order";

if ($complete != 1)
$menu_left = mysql_query($menu);
while ($menurow == mysql_fetch_array($menu_left)) {

if ($menurow['menu_show'] == 1) {

$menu_link = $menurow['menu_link'];
$menu_name = $menurow['menu_name'];

} else {
echo "";
};
$tpl->assign(
array(
'menu_link' => $menu_link,
'menu_name' => $menu_name,
)
);

}
$tpl->parse('MAIN', array("main"));
$tpl->fastprint();


BingBongWongFooey - Its only printing out one thing to my template, i understand how its nothing to do with template but im trying to get it to print out more than one thing to my template.
 

kt

Diamond Member
Apr 1, 2000
6,015
1,321
136
I am not sure what you ultimately want to achieve. From what I can gather, I suppose you want to create a dynamic table of items within a template. If that's the case, you'll need to look into these functions:

$tpl->define()
$tpl->define_dynamic()

The first function allows you to define the template files you'll be using. The second function allows you to define a dynamic block that lives inside a template. Basically, a template within a template. The dynamic block would be the template for your table of items.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |