ok, I just can't think today for some reason. and this has been in the back of my mind for a month or 2.
say I did a query,
select title,question from table order by section
ok so section is a number from 1-4. now say that each section has a title and a number of questions. now thsi is not a quiz but this is a prime example.
so how do I display it so the questions go with the title?
title
question
question
title2
question2
question2
etc..
how can I tell when the section changes because I would like to seperate them.
did I explain that good enough? you don't have to supply code if you can give a good theory behind it.
I was thinking, get teh section number and store it in another variable then just check it on every loop but this doesn't work for some reason.are you able to use the title as a variable and do something like...
SELECT question FROM table WHERE title=blah ORDER BY sectionHmm, how are the questions stored?
Would the table look like this, with the title duplicated in every row, or would you have a seperate table for questions that would relate to the titles-sections table using the section number? Or would the questions all be stored in one string?
title1 | question1 | section1
title1 | question2 | section1
title1 | question3 | section1
title2 | question1 | section2
title2 | question2 | section2
title2 | question3 | section2
or
title1 | section1
title2 | section2
connected to:
section1 | question1
section1 | question2
section1 | question3
section2 | question1
section2 | question2
section2 | question3
or
title1 | question1, question2, question3 | section1
title2 | question1, question2, question3 | section2
??
This would need to be known, if I've understood the question correctly...I've had to do something similar...but I've now replaced that part of the code.but what I did was use the SELECT query like you have,on the first loop defined the variable to check the section against with an if statement..
if(!isset($curcat) || $curcat != $row["sid"])
{
$curcat = $row["sid"];
echo $curcat;
}
elseif(isset($curcat) && $curcat == $row["sid"])
{
echo ''.$row["question"].'';
}
although I think you mensioned that you tried that.....nothing else comes to mind atm..
hmm....Yeah that was my first idea as well, willamoose, if the questions are stored like in my first example.. But why wouldn't it work? What went wrong scoutt? I would suggest a change to my second example btw, to avoid wasting space
Edit:
Wouldn't that code you just posted skip the first question in every section, willamoose? echo $row['title']."\n".$row['question']; EDIT (WHY IS THIS PHP NOT SHOWING?)
OK...took my message out as I couldn't get my PHP to show on the screen. I tried using the PHP BBcode tag and it was croping my message.
Summary of my previous failed post:
All I do is make check on every iteration of the loop to see if the title has changed (if sttement). If it has then I echo the title.ok, sorry for the delay, forum was down
well the table is setup like so
question | answer | section
title is just a blank row, for now. so say 5 questions apiece for each section.
now for each section I hard code a title. so my initial query will not work as title is not part of the table.
without doing a second query I don't think this is possible. I can't tell how amny per section so I can loop that many times. I tried what you did Will but it passes it every time so it made no difference.
I ahve written another query inside this one so it will have to do. but if you have another suggestion I am all ears.
say I did a query,
select title,question from table order by section
ok so section is a number from 1-4. now say that each section has a title and a number of questions. now thsi is not a quiz but this is a prime example.
so how do I display it so the questions go with the title?
title
question
question
title2
question2
question2
etc..
how can I tell when the section changes because I would like to seperate them.
did I explain that good enough? you don't have to supply code if you can give a good theory behind it.
I was thinking, get teh section number and store it in another variable then just check it on every loop but this doesn't work for some reason.are you able to use the title as a variable and do something like...
SELECT question FROM table WHERE title=blah ORDER BY sectionHmm, how are the questions stored?
Would the table look like this, with the title duplicated in every row, or would you have a seperate table for questions that would relate to the titles-sections table using the section number? Or would the questions all be stored in one string?
title1 | question1 | section1
title1 | question2 | section1
title1 | question3 | section1
title2 | question1 | section2
title2 | question2 | section2
title2 | question3 | section2
or
title1 | section1
title2 | section2
connected to:
section1 | question1
section1 | question2
section1 | question3
section2 | question1
section2 | question2
section2 | question3
or
title1 | question1, question2, question3 | section1
title2 | question1, question2, question3 | section2
??
This would need to be known, if I've understood the question correctly...I've had to do something similar...but I've now replaced that part of the code.but what I did was use the SELECT query like you have,on the first loop defined the variable to check the section against with an if statement..
if(!isset($curcat) || $curcat != $row["sid"])
{
$curcat = $row["sid"];
echo $curcat;
}
elseif(isset($curcat) && $curcat == $row["sid"])
{
echo ''.$row["question"].'';
}
although I think you mensioned that you tried that.....nothing else comes to mind atm..
hmm....Yeah that was my first idea as well, willamoose, if the questions are stored like in my first example.. But why wouldn't it work? What went wrong scoutt? I would suggest a change to my second example btw, to avoid wasting space
Edit:
Wouldn't that code you just posted skip the first question in every section, willamoose? echo $row['title']."\n".$row['question']; EDIT (WHY IS THIS PHP NOT SHOWING?)
OK...took my message out as I couldn't get my PHP to show on the screen. I tried using the PHP BBcode tag and it was croping my message.
Summary of my previous failed post:
All I do is make check on every iteration of the loop to see if the title has changed (if sttement). If it has then I echo the title.ok, sorry for the delay, forum was down
well the table is setup like so
question | answer | section
title is just a blank row, for now. so say 5 questions apiece for each section.
now for each section I hard code a title. so my initial query will not work as title is not part of the table.
without doing a second query I don't think this is possible. I can't tell how amny per section so I can loop that many times. I tried what you did Will but it passes it every time so it made no difference.
I ahve written another query inside this one so it will have to do. but if you have another suggestion I am all ears.