Data Looping from MYSQL Database - php

I am having a problem of data looping here. Basically the questions keep looping whenever there are answers for it . I am trying to display something like :
Question
Answer 1
Answer 2
Answer 3
instead of
Question
Answer 1
Question
Answer 2
Question
Answer 3
Anyone know how to solve this ?
<?php
$auctionSurvey = "SELECT questions.question_id, answers.question_id, answers.survey_id, question_body, answer_body FROM questions
INNER JOIN answers ON answers.question_id = questions.question_id
WHERE answers.survey_id='1'";
$aucResult = mysql_query($auctionSurvey) or die (mysql_error());
while ($auctionRow = mysql_fetch_assoc($aucResult)) {
echo $auctionRow['question_body'] . $auctionRow['answer_body'];
}

$questionId = 0;
while($auctionRow = mysql_fetch_assoc($aucResult)){
if($auctionRow['question_id'] != $questionId){
echo $auctionRow['question_body'];
$questionId = $auctionRow['question_id'];
}
echo $auctionRow['answer_body'];
}
Add some HTML to format it and that should work for ya.
Edit: Proof: http://ideone.com/Gkq2hd

Related

Multiple loops in flat database

http://projects.ourplanet.tk/junetxtdb/
http://code.google.com/p/junetxtdb/ in cms (LekkiCMS)
Now my question is how change this code :
$query = $db->select('wizyty');
foreach($query as $record) {
$idz = $record['pro'];
$query2 = $db->select('wizyty_zabiegi',array('id'=>$idz));
foreach($query2 as $record2) {
$record2['name'];
}
}
Every table has lots of values
table wizyty has organized this way
ID"PID"pro"date"time"value1"value2
1"1"2"2020-12-30"12:00"1"2
2"1"1"2020-05-30"12:00"1"2
and table wizyty_zabiegi this way
ID"name"TIMES"HOW
1"Masaż stóp"25"100
2"Masaż nóg"25"100
i use even for (valuepro = valueid)
but this show only good in first result
third, five , six etc result dont show correctly
thank you ;)
i know in mysql i have join but in this project i dont have it ;(
i wrote this question and i found solution hahah
$is= $record['zabieg'] -1;
$query2 = $db->select('wizyty_zabiegi');
$result .= $query2[$is]['nazwa'];
thanks for everything even for reading this messages

Eloquent WhereHas With Several Nested Wheres from Array

I have a database with tables: questions, answers, responses, and respondents. Questions have answers, which have responses, which are owned by a respondent (Respondents can have many responses).
I'm trying to make correlations by zeroing in on all the responses to questions related through respondents who chose a certain answer to a question. So, what else did people who answered that they were 18 - 20, answer.
The trick is I want to do this across multiple respondents and multiple answers. For example, what else did people who are 18 - 20, and like the color green like:
I currently have this working in an "or" fashion where each additional answer_id I add pulls in respondents who chose either one of the two answers:
$cFilters = array() of answer_ids;
$respondents = Auth::user()->account->respondents()
->whereHas('responses', function($q) use($cFilters) {
$q->whereIn('responses.answer_id', $cFilters);
})->get();
I'd ultimately like to have them working in an "and" fashion where adding in another answer_id shows only respondents who have chosen both answers identified by the answer_ids, can't quite figure that one out. Here is what I've tried so far that doesn't raise errors, but also doesn't seem to work correctly:
$cFilters = array() of answer_ids;
$respondents = Auth::user()->account->respondents()
->whereHas('responses', function($q) use($cFilters) {
foreach($cFilters as $answer_id) {
$q->where('responses.answer_id', $answer_id);
}
})->get();
I've had to resort to raw SQL, but it works:
$query = 'SELECT * FROM respondents
WHERE id IN ( ';
$i = 1;
foreach($cFilters as $answer_id) {
$query = $query . 'SELECT respondent_id FROM responses WHERE answer_id=' . $answer_id;
if($i < count($cFilters)) {
$query = $query . ') AND id IN (';
}
$i++;
}
$query = $query . ')';
$respondents = DB::select( DB::raw($query) );

PHP MySQL Get Specific Value When Result Show [duplicate]

This question already has answers here:
Extracting Twitter hashtag from string in PHP
(7 answers)
Closed 8 years ago.
How to get specific value of query in MySQL.
My code :
$q = mysqli_query("SELECT * FROM tb_post WHERE post LIKE '%#%'");
while($d = mysqli_fetch_array($q))
{
$post = $d['post'];
}
Example data result like this :
I'm groot #me
What are you doing #what
Sometimes I felt like superman #me
I don't know what to do #confuse
Now, my question is : is it possible to get just hashtag? Example : #me, #what, #confuse.
So I do not need another value.
Please help!
This should work for you:
$post = $d['post'];
$whatIWant = substr($post, strpos($post, "#") + 1);
echo "#" . $whatIWant . "<br />";
Output:
#me
#what
#me
#confuse

Data insertion using array in PHP

I have a php script for conducting online test. while taking test, I am using post method to get the answers in the action page.The answers are passed as an array with question id and the selected option value(true or false/the answers the candidate entered like that).Then I have to insert the answers marked by the candidate in the database. The code is as shown below:
$student_id=$_POST['name'];
$survey_id=$_POST['survey_id'];
$store = array();
if (isset($_POST['question_id'])) {
foreach ($_POST['question_id'] as $key => $option) {
$option1 = array_filter($option);
print_r($option1);
if (count($option1) > 1) {
$option2 = implode("^", $option1);
$store[] = $option2;
} else {
$value = $option1;
$i = implode(null, $option1);
$store[] = $i;
}
}
print_r($store);
}
$t = new DateTime();
$t->setTimestamp($time = time());
$t->setTimeZone(new DateTimeZone("Asia/Singapore"));
$date = $t->format(DateTime::RFC850);
$SQL = "INSERT INTO answer_table(student_id ,survey_id, ans_1, ans_2, ans_3, ans_4, ans_5, ans_6, ans_7, ans_8, ans_9, ans_10, timestamp) VALUES ('$student_id','$survey_id', '$store[0]', '$store[1]', '$store[2]', '$store[3]', '$store[4]', '$store[5]', '$store[6]', '$store[7]', '$store[8]', '$store[9]', '$date')";
$result = mysql_query($SQL);
If the student answers the test in sequential order(from question number 1 to 10) the code works fine. But when the candidate answers in random manner(first 10 then 5 like that),the table field named,ans_1 will get inserted with answer of question number 10. I need to insert fields with corresponding answers ,(ans_1 with answer of question 1 like that) what ever pattern,the candidate takes test.
Can anyone help me to solve this issue. Thanks in advance.
For achieving you goal.you should store question_id with answers or create another table which store question_id and corresponding answers, with id of answer table.
If you have the question id in the $key variable, you could use
$store[$key] = $option2;
instead of
$store[] = $option2;
With that the first question is the first entry in the array regardless of the order of the answers in the post array.
You can replace $store[] = $option2; with:
$store[$_POST['question_id']] = $option2;

Adding pagination to my mysql query [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP & MySQL Pagination
I'm a bit confused as to how to add pagination to my mysql query, since I am looping through the results and adding them to a variable each time:
$DBQuery3 = mysqli_query($dblink, "SELECT * FROM images WHERE project_id = '$FormProjectID'");
$ProjectContent ='';
while($row = mysqli_fetch_array($DBQuery3)) {
$DBImageID = $row['image_id'];
$DBProjectID = $row['project_id'];
$DBImageName = $row['image_name'];
$DBImageDescription = $row['image_description'];
$DBDateCreated = $row['date_created'];
$DBLinkToFile = $row['link_to_file'];
$DBLinkToThumb = $row['link_to_thumbnail'];
$DBGivenName = $row['given_name'];
//if the image was given a name by the user, display it
//otherwise display the generated name
if (strlen($DBGivenName) > 1) {
$FileName = $DBGivenName;
} else {
$FileName = $DBImageName;
}
$ProjectContent .= '
<img src="uploads/'.$DBLinkToThumb.'" width="150px" height="150px" alt="'.$FileName.'" title="'.$FileName.'"/>
';
Any pointing in the right direction would be greatly appreciated.
Hy,
Try use a php pagination script, there are a lot on the net, just search for those words.
For example this: http://www.coursesweb.net/php-mysql/pagination-class-script_s2 can paginate data stored into a database, or into an array.

Categories