PDA

Click to See Complete Forum and Search --> : Loop through database...


Muk108
Mar 12th, 2003, 07:18 PM
Ok im back again and i am still trying to loop through my database in a template. is this how i would loop through it? i need to loop through the ID of the item first then while its still doing that print out the data?

so

loop {

$dbid

loop {

$dbtext

}

}

or something like that? do you think you could help me put this in php?

phpman
Mar 12th, 2003, 08:28 PM
loop through the id first?? huh you can't do that.

just loop through the whole table do this


$query=("select * from table")

while ($row= musql_fetch_array($query){
echo $row["id"];
echo $row["field1"];
}


that will loop through the whole db and display every ID and FIELD1

so what are you having trouble with? you said a template?

Muk108
Mar 13th, 2003, 02:35 PM
well im trying to use a template engine like fasttemplate so i can have someone do the html and stuff and i just work on the php part. but whenever i do it, it only displayes one item from the database.

The Hobo
Mar 13th, 2003, 02:49 PM
Originally posted by Muk108
well im trying to use a template engine like fasttemplate so i can have someone do the html and stuff and i just work on the php part. but whenever i do it, it only displayes one item from the database.

You'd probably have to post your code before we can find the bug...

Muk108
Mar 13th, 2003, 04:01 PM
<?
include("db.php");
include("class.FastTemplate.php3");
if ($complete != 1)
$news = mysql_query($query);
while ($row = mysql_fetch_array($news)) {
$title_text = $row['main_title'];
};
//

$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 "";
};
}

//
$newsquery = "SELECT * FROM news";

if ($complete != 1)
$news = mysql_query($newsquery);
while ($row = mysql_fetch_array($news)) {
$news_title_text = $row['news_title'];
$news_message_text = $row['news_message'];
$news_submitter_text = $row['news_submitter'];
$news_date_text = $row['news_date'];
};
//

$tpl = new FastTemplate("./templates");
$tpl->define(array(
'main' => "index.tpl",
)
);
//

$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,
'menu_link' => $menu_link,
'menu_name' => $menu_name,
'message_area' => $message_area,)
);
$tpl->parse('MAIN', array("main"));

$tpl->FastPrint();
//$tpl->showDebugInfo();
exit;
?>

thats what i have, i have been working with this for a long time, hope you can help me.

phpman
Mar 13th, 2003, 04:44 PM
is this it?

while ($row = mysql_fetch_array($news)) {
$news_title_text = $row['news_title'];
$news_message_text = $row['news_message'];
$news_submitter_text = $row['news_submitter'];
$news_date_text = $row['news_date'];
};


that will only get the last info from teh db, so the last id will only be sent. I thought we fixed this a while ago. :confused:

Muk108
Mar 13th, 2003, 04:46 PM
no, i dont think we did, or we did and when i tryed putting it in it didnt work. could you help me with this one?

The Hobo
Mar 13th, 2003, 05:26 PM
This is a problem right here:

while ($menurow == mysql_fetch_array($menu_left)) {

Should only be one = sign.

Muk108
Mar 13th, 2003, 05:37 PM
that doesnt seem to matter, i still only get one resault printed out on the page.

The Hobo
Mar 13th, 2003, 09:03 PM
Originally posted by Muk108
that doesnt seem to matter, i still only get one resault printed out on the page.

It may not fix your problem, but yes, it does matter, as that's not how you do it.

Muk108
Mar 13th, 2003, 09:20 PM
ok well, thank you. i have tryed a for loop at $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,
'menu_link' => $menu_link,
'menu_name' => $menu_name,
'message_area' => $message_area,)
);
this point. but still no luck.

The Hobo
Mar 13th, 2003, 09:41 PM
I think phpman hit the nail on the head. You need to do something like this:

<?php
include("db.php");
include("class.FastTemplate.php3");

if ($complete != 1)
$news = mysql_query($query);
while ($row = mysql_fetch_array($news)) {
$title_text[] = $row['main_title'];
}
//

$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 "";
}
}

$newsquery = "SELECT * FROM news";

if ($complete != 1)
$news = mysql_query($newsquery);

while ($row = mysql_fetch_array($news)) {
$news_title_text[] = $row['news_title'];
$news_message_text[] = $row['news_message'];
$news_submitter_text[] = $row['news_submitter'];
$news_date_text[] = $row['news_date'];
}

$tpl = new FastTemplate("./templates");
$tpl->define(array('main' => "index.tpl",));
// ^-- what's that comma doing in there?


for ($i = 0; $i < count($title_text); $i++) {
$tpl->assign(
array(
'title' => $title_text[$i],
'news_title_text' => $news_title_text[$i],
'news_message_text' => $news_message_text[$i],
'news_submitter_text' => $news_submitter_text[$i],
'news_date_text' => $news_date_text[$i],
'menu_link' => $menu_link[$i],
'menu_name' => $menu_name[$i],
'message_area' => $message_area[$i],)
//^--what's up with that last comma?
);

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

$tpl->FastPrint();
//$tpl->showDebugInfo();
}

exit;
?>

Give it a try. I believe it will work

phpman
Mar 13th, 2003, 11:31 PM
exactly, Hobo.

see that is what you needed to do, Muk108. jsu tlike this thread where you just left it hanging

http://www.vbforums.com/showthread.php?s=&threadid=228334

you never said it didn't work and we told you what the problem was. and again you ask the same question and got the same answer.

The Hobo
Mar 14th, 2003, 07:18 AM
Originally posted by phpman
see that is what you needed to do, Muk108. jsu tlike this thread where you just left it hanging

http://www.vbforums.com/showthread.php?s=&threadid=228334

you never said it didn't work and we told you what the problem was. and again you ask the same question and got the same answer.

I guess when you don't like the answer, you can ask again later and see if you get a different one.

Sadly, for him, this is the answer.

Muk108
Mar 15th, 2003, 08:07 PM
im sorry i havent replyed, i was buisy the last two days. and when i execute that i go this error....

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\HTTP\www\intra\index.php on line 71

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\HTTP\www\intra\index.php on line 81


and it just loops through that locking up my server.

Muk108
Mar 15th, 2003, 08:23 PM
ok i got the page to load, but now for ME, it doesnt want to loop through and print it out again...

The Hobo
Mar 17th, 2003, 05:54 PM
Originally posted by Muk108
ok i got the page to load, but now for ME, it doesnt want to loop through and print it out again...

I don't know what to tell you anymore. Try finding some basic tutorials first. Or maybe programming concepts.

Not to overuse a cliche, but we can only lead you to water...

Muk108
Mar 17th, 2003, 07:38 PM
i understand i have been looking at tutorials and havnt found anything. ill keep looking and when i find it ill post it. thanks for all your guys help.

phpman
Mar 17th, 2003, 09:40 PM
my guess is that you messed up the query when you got the mysql_fetch_arrary errors.