|
-
Feb 1st, 2003, 06:14 PM
#1
Thread Starter
Hyperactive Member
Problem looping through database.
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...
PHP Code:
$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...
Code:
<a href="{menu_link}.php">{menu_name}</a>
can you help me with this one?
-
Feb 2nd, 2003, 01:10 PM
#2
Frenzied Member
that is becasue every time you loop throught the db it resets teh names
if ($menurow['menu_show'] == 1) {
$menu_link = $menurow['menu_link'];
$menu_name = $menurow['menu_name'];
} else {
echo "";
};
that is saying if menu_show equals 1 then menu_link equal menurow[], so it comes to the next one and if it equals to 1 it will overwrite?
so you hav eto put them into the array first like so
if ($menurow['menu_show'] == 1) {
$menu_link[] = $menurow['menu_link'];
$menu_name[] = $menurow['menu_name'];
} else {
echo "";
};
that is the only way to do it if you want more than 1
Last edited by phpman; Feb 2nd, 2003 at 01:13 PM.
-
Feb 2nd, 2003, 01:18 PM
#3
Frenzied Member
actually you can do this
PHP Code:
$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) {
$tpl->assign(array(
'menu_link' => $menurow['menu_link'],
'menu_name' => $menurow['menu_name']));
}
}
-
Feb 2nd, 2003, 09:56 PM
#4
Thread Starter
Hyperactive Member
thats not printing anything out to where there was one item from the database.
-
Feb 3rd, 2003, 12:20 AM
#5
Frenzied Member
-
Feb 3rd, 2003, 06:28 AM
#6
Thread Starter
Hyperactive Member
-
Feb 3rd, 2003, 11:30 AM
#7
Frenzied Member
I don't see anything wrong with it. what is the assigns function?
-
Feb 3rd, 2003, 12:19 PM
#8
Stuck in the 80s
Code:
$menu = "SELECT * FROM menu_left ORDER BY menu_order";
if ($complete != 1)
$menu .= " LIMIT 20";
$menu_left = mysql_query($menu) or die(mysql_error());
while ($menurow = mysql_fetch_array($menu_left)) {
if ($menurow['menu_show'] == 1) {
$tpl->assign(array(
'menu_link' => $menurow['menu_link'],
'menu_name' => $menurow['menu_name']));
}
}
Always throw that line on there. PHP will not always report a MySQL error.
I always do this to check to see if the SQL worked properly:
Code:
if ($menurow = mysql_fetch_array($menu_left)) {
do {
if ($menurow['menu_show'] == 1) {
$tpl->assign(array(
'menu_link' => $menurow['menu_link'],
'menu_name' => $menurow['menu_name']));
}
}while ($menurow = mysql_fetch_array($menu_left));
} else {
echo "No records found!";
}
Otherwise the problem lies in whatever the assign() function does, as phpman has asked.
-
Feb 3rd, 2003, 03:24 PM
#9
Thread Starter
Hyperactive Member
ok that did work either, hobo. how can i just test to see if we can get the loop to work without the database. then put in the databsae and get that working.
-
Feb 3rd, 2003, 03:39 PM
#10
Stuck in the 80s
Originally posted by Muk108
ok that did work either, hobo. how can i just test to see if we can get the loop to work without the database. then put in the databsae and get that working.
What do you mean it doesn't work? Do you get an error? Do you get the "No records found!" message? What do you get? Nothing? Simply saying it doesn't work isn't going to help us find a solution.
And You can't get the loop to work without the database. The loop runs based on whether or not you have stuff from the database.
If the code I gave you gives no errors, then the problem must lie in whatever assign() is doing.
Run this code. See if you get output:
Code:
$menu = "SELECT * FROM menu_left ORDER BY menu_order";
if ($complete != 1)
$menu .= " LIMIT 20";
$menu_left = mysql_query($menu) or die(mysql_error());
while ($menurow = mysql_fetch_array($menu_left)) {
if ($menurow['menu_show'] == 1) {
/*$tpl->assign(array(
'menu_link' => $menurow['menu_link'],
'menu_name' => $menurow['menu_name']));*/
echo "<b>Test</b>: " . $menurow['menu_name'] . "<br>\n";
}
}
If you do not get any output, then it is a problem with the database. If you do get output, then it is a problem with your assign() function.
-
Feb 3rd, 2003, 03:55 PM
#11
Frenzied Member
try this and see what it says.
PHP Code:
$menu = "SELECT * FROM menu_left ORDER BY menu_order";
if ($complete != 1) {
$menu .= " LIMIT 20";
}
$menu_left = mysql_query($menu);
if (mysql_num_rows($menu_left)){
while ($menurow = mysql_fetch_array($menu_left)) {
if ($menurow["menu_show"] == "1") {
$tpl->assign(array("menu_link" =>$menurow["menu_link"], "menu_name" =>$menurow["menu_name"]));
}
}
} else{
echo "nothing found in db";
}
-
Feb 3rd, 2003, 04:03 PM
#12
Thread Starter
Hyperactive Member
that only returned one resault, as did the last 2.
-
Feb 3rd, 2003, 04:05 PM
#13
Stuck in the 80s
Originally posted by Muk108
that only returned one resault, as did the last 2.
How many results should there be?
You're messing something up, dude. We can't tell you the magical password to get your script to work. You need to step through it and figure out what's going on. We can't do that for you.
-
Feb 3rd, 2003, 04:18 PM
#14
Frenzied Member
could it be possbile that ther is only one row that has a $menurow["menu_show"] of 1 in the db???
-
Feb 3rd, 2003, 07:18 PM
#15
Thread Starter
Hyperactive Member
It only displays the last one. There should be 8 results. do you know what im doing wrong that it only displayes the last item?
-
Feb 3rd, 2003, 07:43 PM
#16
Frenzied Member
I believe that assigns() will only do 2.
what is the assign() function? and what is the code you have now?
-
Feb 3rd, 2003, 08:39 PM
#17
Thread Starter
Hyperactive Member
this is what it said on the site....
The method assign() assigns values for variables. In order for a variable in a template to be interpolated it must be assigned. There are two forms which have some important differences. The simple form, is to accept an array and copy all the key/value pairs into an array in FastTemplate. There is only one array in FastTemplate, so assigning a value for the same key will overwrite that key.
$tpl->assign(TITLE => "king kong");
$tpl->assign(TITLE => "godzilla"); // overwrites "king kong"
this is what i currently have as the second assign()...
PHP Code:
$tpl->assign(
array(
'title' => $title_text,
'news_title_text' => $news_title_text,
'news_message_text' => $news_message_text,
'news_submitter_text' => $news_submitter_text,
'news_date_text' => $news_date_text,
'message_area' => $message_area,)
);
then i have what has been sudjested.
-
Feb 3rd, 2003, 09:06 PM
#18
Frenzied Member
Originally posted by Muk108
this is what it said on the site....
The method assign() assigns values for variables. In order for a variable in a template to be interpolated it must be assigned. There are two forms which have some important differences. The simple form, is to accept an array and copy all the key/value pairs into an array in FastTemplate. There is only one array in FastTemplate, so assigning a value for the same key will overwrite that key.
$tpl->assign(TITLE => "king kong");
$tpl->assign(TITLE => "godzilla"); // overwrites "king kong"
did you just not read what you just said???????
There is only one array in FastTemplate, so assigning a value for the same key will overwrite that key.
-
Feb 3rd, 2003, 10:10 PM
#19
Thread Starter
Hyperactive Member
sorry, i miss read what you said then. i try and read things to fast. what template do you sedjest i can use then to do what im trying to do?
-
Feb 3rd, 2003, 10:24 PM
#20
Stuck in the 80s
-
Feb 3rd, 2003, 10:54 PM
#21
Frenzied Member
Originally posted by Muk108
sorry, i miss read what you said then. i try and read things to fast. what template do you sedjest i can use then to do what im trying to do?
make your own, a good learning experience
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|