-
Posts
6,906 -
Joined
-
Last visited
-
Days Won
99
Everything posted by ginerjm
-
OK - I should have echo'ed the $q inside the test of $last_q. This is the change: if($q <> $last_q) // starting a new question so close the last div and begin a new one { if ($last_q <> 'x') // is this the first question? If not, close the last question's box echo "</div>"; echo "<div class='question_box'>"; // start the new question box echo "<p>$q</p>"; // show the question - figure out later how to prevent repetition (which I did earlier) $choice_num = 0; // new question; new choice count } Remove the echo of $q from below this. Of course the css for the choice box should have (perhaps) and indent to position the answers neatly below the question. Maybe a label to identify the choice as well?
-
After taking your code and cleaning it up a bit I cannot find any place that defines that $return_link variable. I see it being used but never defined. And why the concern for php 4? You should not need that. Where the error occurs I would add an echo of that value to see what it is.
-
The first line does what? So what is the second line supposed to do for you?
-
Your sample code is going to give an error every time it runs.
-
So you are putting the question in one box with 4 boxes inside of that box to show the current row's choice and the next 3 rows choices below the 1st. I gave you this code before and you have lost it. I don't think you understand how the results are coming to you and I think you need to do a print_r on one of the rows to get a visual look at what you have to work with. My original code will handle this. It just didn't have the css which I did mention would have to be added. Perhaps if you run the following code (assuming that I have no typos) it will give you a clear view of the data and then you can work with it to design it properly. Then you will see why I had that $last_q var in there. include 'includes/db.php'; $sql = "SELECT q.question, o.choice FROM questions q JOIN quiz_options o ON q.qid = o.qid WHERE q.qid = 1;"; $results = $pdo->query($sql); // this produces rows that contain a question and a choice for that question. // That means you have to handle the multiple choices by NOT showing the same question over and over. $last_q = 'x'; // a non-existent value $choice_num = 0; while(list($q, $c) = $results->fetch(PDO::FETCH_NUM)) // retrieve the contents of a single row as 2 vars { if($q <> $last_q) // starting a new question so close the last div and begin a new one { if ($last_q <> 'x') // is this the first question? If not, close the last question's box echo "</div>"; echo "<div class='question_box'>"; // start the new question box $choice_num = 0; // new question; new choice count } $choice_class = 'choice' . $choice_num; // setup the appropriate choice class $last_q = $q; // remember the current question echo "<p>$q</p>"; // show the question - figure out later how to prevent repetition (which I did earlier) echo "<div class='$choice_class'>"; // a box for this choice echo $c; echo "</div>"; // close this choice's box $choice_num++; // bump the choice cnt } echo "</div>"; // close the last question box If you don't want to pursue this effort I understand. It's your task and your option and I'll drop out.
-
Here is how I see your code with my questions interspersed. include 'includes/db.php'; $sql = "SELECT questions.question, quiz_options.choice FROM questions JOIN quiz_options ON questions.qid = quiz_options.qid WHERE questions.qid = 1;"; $statement = $pdo->query($sql); // query returns some rows each having 1 questions and 1 choice on it $questions = $statement->fetchAll(PDO::FETCH_GROUP); // never seen this option before // ps - all a fetchall does is copy your results to an array when // you could just loop thru the results a row at a time. Same result just a waste of memory // begin a loop foreach($questions as $question=>$group) // $question will be a numerical index of your array { echo "<div class='quizContent'>"; // one css setting for the entire q/a box? echo "<p>$question;?></p>"; $choice_styles = ['answerA', 'answerB', 'answerC', 'answerD']; // css class names? foreach($group as $index=>$row) // What is $group supposed to contain? echo "<div class='$choice_styles[0]'> ".$row["choice"]." </div>"; // you are using the SAME class name for every time thru // also you never show how $row got created } // end of loop Have you actually looked at your query results? It might make some of these issues go away if you see what your query gives you. Can we see these answera, answerb... css classes too? I want to see what you are doing that you need multiples
-
If your foreach is looping thru the query results what is that "key=>value" pairing supposed to represent? A query results doesn't have a key value on each row so I don't know what you think you getting there. And - Why do you need a unique class definition for the different question answers? Can't you just use one class? Showing the entire code might be helpful
-
Curious why you think you need a loop to add the css/html. The loop is already there. You just need to do the formatting. I would add a div to begin the question box and when I output the choice I would add that inside that first box and close it. Then add a second one for the next choice. And when I had to show a new question, I would end the first question box and begin the new question box. No new loop needed?
-
I used last_q to only show the question once. I disagree with your thought on switching modes being easier at any time. FWIW - my scripts, short or long start PHP mode at the top and never end it. I never, EVER use the end-php-tag in my code. Never. When I have to output lots of non-php code I use the php Heredocs command. Try it - I think you'll like it.
-
But did you learn why your original code was not doing the job? That's the Important thing. Hope you learned something about how my code worked to make it easier for you the next time. Staying in php mode and using echo lines is so much better (IMHO) than leaving php mode and having to switch in and out while writing your hmtl is so much more complicated.
-
Here is my much-abbreviated copy of your code. Let's see what you get from it without all of the css and the needless fetchall. $sql = "SELECT a.question, b.choice FROM questions a, quiz_options b where a.qid = b.qid"; if (!$results = $pdo->query($sql)) { echo "Could not run query: $sql"; exit(); } $last_q = 'x'; while ($row = $qresults->fetch()) { if ($last_q <> $row['question']) { echo $row['question']; } $last_q = $row['question']; echo $row['choice'] . '<br>'; } I'm trying to give you something that displays the data just to be sure that your query is providing what you think it is.
-
My isset() function runs before i click the button
ginerjm replied to GamesBond008's topic in PHP Coding Help
If you sit back and look at what you have written it might just come to you. You execute the php code BEFORE even send the html to the client. OF Course it is going to tell you things are empty because at that time THEY ARE. -
Possible To Add Sums Of Many Tbl Cols With Sql ?
ginerjm replied to TheStudent2023's topic in PHP Coding Help
One does NOT put multiple 'same' values into one record. AS MY POST DEMONSTRATED. One user may have 3-4-5-or-even-10 rows in table #3 if that is what it takes. He does not have one row with 10 keywords in it. Period. -
Possible To Add Sums Of Many Tbl Cols With Sql ?
ginerjm replied to TheStudent2023's topic in PHP Coding Help
You should have a table of 'users' where you put my name and some other 'personal' info pertaining to me alone. Then you should have a keywords table containing all possible keywords along with their point value (which I am sure you will continue to add new records as you come up with new keywords) and lastly a table that contains usernames (as specified in table 1) and keywords from table 2. Then you do a query for a username and all of his assigned keywords from table 3 that does your summing. -
Possible To Add Sums Of Many Tbl Cols With Sql ?
ginerjm replied to TheStudent2023's topic in PHP Coding Help
No you did not. Of course - I Know it is a column. My question pertains to "are you putting multiple values of something into a single record". A table should contain individual values/attributes of something in one record. Where there are multiple values of that attribute (such as a keyword?) they should be in their own table, tied to the primary via a key such as the primary id/key. -
Possible To Add Sums Of Many Tbl Cols With Sql ?
ginerjm replied to TheStudent2023's topic in PHP Coding Help
Are kw1, kw2, kw3..... the same piece of data? Like phone1 phone2 phone3? Cause if they are you have a bad database design. -
My isset() function runs before i click the button
ginerjm replied to GamesBond008's topic in PHP Coding Help
How is this script executed the first time? From the address bar of your web page? Or from a command that you issue? Obviously it has to be started somehow and that means that you either triggered it from a submit that is from a form that was sent to the user by a different script. If not that is your problem. The form in this script is not being sent until your code runs and that is your query. Also - Your form only contains a text field and no buttons. And I wouldn't name my text field 'submit' since it is NOT a submit button. The default value of an input tag is 'text' I believe. -
Seems odd to me that you want to hide a button that is submitting a form and your php script could do the hiding before the client is refreshed from the php processing. This form goes away when you click on the button so why are you worrying about hiding it?
-
I see a whole lot of arrays in your multiple posts. Which one are you concerned with 'looping through'? Assuming the query results array would be my thought but I don't know why that is the question since you have already begun a loop to go thru that one. So - it must be one of your very own arrays and if you are having trouble looping thru any of them how about just not making it so complex? Haven't seen the query string so don't know how complex your database is but if you are managing to gather all the data you need into a readable query result, then why make all those arrays upon arrays upon arrays?