PDA

View Full Version : How Do You Do A Loop?


Craigr
04-05-2003, 11:48 AM
// Quiz Addon

$quiznamez=$DB_site->query_first("SELECT quiztitle, description, quizid, timestamp FROM quiz ORDER BY quizid ASC LIMIT 10");

$quizresults=$DB_site->query_first("SELECT totalcorrect, resulttime FROM quiz_results WHERE userid = '$userinfo[userid]' ORDER BY quizid ASC LIMIT 10");

$totalquestions=$DB_site->query_first("SELECT COUNT(quizid) AS questions FROM quiz_questions WHERE quizid='$quiznamez[quizid]'");
$questions = $totalquestions[questions];

$numA = $quizresults[totalcorrect];
$numB = $questions;
$percentage = $numA/$numB*100;

$dateposted = date("D j M Y, g:i A",$quiznamez[timestamp]);
$taken = date("D j M Y, g:i A",$quizresults[resulttime]);

$quizname .="<tr><td bgcolor='#F1F1F1' width=20%><normalfont><b>$quiznamez[quiztitle]</b></normalfont> <smallfont>[ <a href=\"quiz.php?s=&quizid=$quiznamez[quizid]\">Take Quiz</a> ]<br><smallfont>$quiznamez[description]<br><i>Created: $dateposted</i></smallfont></td><td bgcolor='#F1F1F1' width=20%><normalfont>$quizresults[totalcorrect] correct from $questions questions. Giving a percentage of $percentage%.</normalfont><br><smallfont><i>Quiz completed: $taken</i></smallfont></td></tr>\n";

// End Quiz Addon


I've been trying for ages now to make the above query loop. This is the first time i've tried to make any SQL.

The above is an addon i was making for the quiz. In the member profile i hope to pull the quiz names and the person's
results and put them at the bottom of the profile.

At the moment the above just pulls one quiz name and one result. I want it to keep going until all the
quiz names/results are displayed. I think you use while, but i'm not sure.

Could someone help? Also could you let me know if any of the above is wrong.

Thanks
Craig

noppid
04-05-2003, 01:08 PM
Get the record count and use a for loop or...

Get the whole table and use a for each loop or...

Get the highest of the QuizID and use it in a for loop.

There's more then one way to skin a cat. But you need to weigh each option for over head and such to decied.

Craigr
04-05-2003, 01:21 PM
Today at 03:08 PM noppid said this in Post #2 (https://vborg.vbsupport.ru/showthread.php?postid=377843#post377843)
Get the record count and use a for loop or...

Get the whole table and use a for each loop or...

Get the highest of the QuizID and use it in a for loop.

There's more then one way to skin a cat. But you need to weigh each option for over head and such to decied.

How would i do any of them. I don't know any SQL i spent about 2 days getting the above to work I created it by looking at other scripts?

Basically i don't know how to do a loop. I've tried, but it doesn't work for me.

Craig

noppid
04-08-2003, 04:53 PM
I'm sorry I didn't get back to ya. I took a closer look and it's not something I could work out without much more detail.

I'm tied up in the middle of a painting nightmare since I last posted. Sorry I couldn't be of more help.

leviw
04-24-2003, 11:57 PM
I'm pretty new to PHP myself so am not much help, but the first thing I think when I see all that is, "No wonder hes confused.."

Use a smaller experiment, meet and marry the php.net website, and try to have little php kids with it if at all possible.

Best way to learn is by taking tiny chunks and tweaking them, so you know what happens when you move each piece. Then go back and add details AFTER you have a working, backed up example.

Good luck. =)

filburt1
04-24-2003, 11:59 PM
Side note: always use vbdate() instead of date() to take a user's GMT shift into effect.

Logician
04-25-2003, 11:23 AM
$quizresultsDB=$DB_site->query("SELECT totalcorrect, resulttime FROM quiz_results WHERE userid = '$userinfo[userid]' ORDER BY quizid ASC LIMIT 10");

while ($quizresults=$DB_site->fetch_array($quizresultsDB))
{
//viewed session
$myquizresults.='Total Correct='.$quizresults[totalcorrect].' - Result Time='.$quizresults[resulttime].'<br>';
}
echo $myquizresults;

Craigr
04-25-2003, 02:13 PM
Thanks all. I was going to use this for the members profile, but the addon has been created already in the quiz thread.

I will have to read my book 'php and mysql for dummies'. :D

Thanks again,
Craig