Displaying Data Randomly in PHP From Database - php

I'm Building a Simple PHP Page for MCQS (Multiple Choice Questions). I'm Using MySQL Database to retrieve data. I want to Display Options / Alternatives ( i.e: field_correct_answer_value, field_wrong_answer_1_value, field_wrong_answer_2_value, field_wrong_answer_3_value, field_wrong_answer_4_value) in Random Order with stylesheet so i can identify which one is correct.
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<hr><div id=q1>". $row["field_question_value"]."</div>";
echo "<div id=a1>". $row["field_correct_answer_value"]."</div>";
echo "<div id=a2>". $row["field_wrong_answer_1_value"]."</div>";
echo "<div id=a2>". $row["field_wrong_answer_2_value"]."</div>";
echo "<div id=a2>". $row["field_wrong_answer_3_value"]."</div>";
echo "<div id=a2>". $row["field_wrong_answer_4_value"]."</div>";
}
Is it Possible?

($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
//FOR RANDOMIZATION OPTION COLLECTING DATA IN ARRAY AND SHUFFLING THEM
$corr = "<div id=a1>" . $row["field_correct_answer_value"] . "</div>";
$wor1 = "<div id=a2>" . $row["field_wrong_answer_1_value"] . "</div>";
$wor2 = "<div id=a2>" . $row["field_wrong_answer_2_value"] . "</div>";
$wor3 = "<div id=a2>" . $row["field_wrong_answer_3_value"] . "</div>";
$wor4 = "<div id=a2>" . $row["field_wrong_answer_4_value"] . "</div>";
$opt = array($corr, $wor1, $wor2, $wor3, $wor4,);
$opt_ran = shuffle($opt);
echo "<hr><div id=q1>". $row["field_question_value"]."</div>";
//FOR DISPLAYING RANDOM OPTIONS
foreach ($opt as $number) {
echo $number;
}
}

Related

How can I post my data that i get from SQL into divs in php

I need to make a page that shows posts from database. It has to have a Title and text in the post. I get all of the information from database already, but I don't know how to make it so they are placed into divs. Currently the data I get is set into tables, but I would like to get them into divs. Current code: (note: I have the php and sql connection working)
$sql = "SELECT title, txt FROM xxxxx";
$result = $con->query($sql);
if ($result->num_rows > 0) {
// output data of each row
echo "<table><tr><th>Title</th><th>Text</th></tr>";
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["title"]. "</td><td>" . $row["txt"]. " </td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
Also by that I was looking for bootstrap panel-group classes, but I was not able to implement that.
It seems you forgot to include your divs:
if ($result->num_rows > 0) {
// output data of each row
echo "<div><table><tr><th>Title</th><th>Text</th></tr>";
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["title"]. "</td><td>" . $row["txt"]. " </td></tr>";
}
echo "</table></div>";
} else {
echo "0 results";
}
or if you don't want to include a table but only the divs, simply do it like so:
if ($result->num_rows > 0) {
// output data of each row
echo "<div class='title'><h1>Title</h1>";
while($row = $result->fetch_assoc()) {
echo "<p>" . $row["title"]. "</p><p>" . $row["txt"]. " </p>";
}
echo "</div>";
} else {
echo "0 results";
}

Grab and echo out multiple SQL rows with PHP

I'm wanting to fill a database with news article information such as PostID, PostTitle, PostDescription, PostContent and so on - I tried writing some code to constantly echo out the data in the SQL query but it only works for the first row that the query picks up
$SQL = "SELECT * FROM posts"; // get all info from posts
$result = mysql_query($SQL);
$row = mysql_fetch_array($result);
if (mysql_numrows($result) > 0) {
$row = mysql_fetch_array($result);
$Title = $row["PostTitle"];
$Description = $row["PostDescription"];
$Date = $row["PostDate"];
$AuthorID = $row["AuthorID"];
echo "<div id='preview'>";
echo "<div class='group selection'>";
echo "<div class='col span_1_of_3'>";
echo "<div id='previewmedia'>";
echo "</div>";
echo "</div>";
echo "<div class='col span_2_of_3'>";
echo "<h1>".$Title."</h1>";
echo "<p class='preview'>".$Description."</p>";
echo "<a href='bloglayout.php' class='orange readmore'>Read More...</a>";
echo "</div>";
echo "</div>";
echo "</div>";
}
I then thought about doing a SELECT query to then count the number of ID's in the table and then set that as $ID and then give each variable a number and then +1 after each loop but realised that wouldn't work so I'm kinda stuck on how to loop out several rows automatically from my database.
Also, I know I shouldn't be using mysql_numrows and whatnot in PHP and I should move to mysqli or PDO but this is for something where I've been asked to use mysql specifically.
You're only fetching one row. Try looping through your results.
$SQL = "SELECT * FROM posts"; // get all info from posts
$result = mysql_query($SQL);
while( $row = mysql_fetch_array($result);)
{
$Title = $row["PostTitle"];
$Description = $row["PostDescription"];
$Date = $row["PostDate"];
$AuthorID = $row["AuthorID"];
echo "<div id='preview'>";
echo "<div class='group selection'>";
echo "<div class='col span_1_of_3'>";
echo "<div id='previewmedia'>";
echo "</div>";
echo "</div>";
echo "<div class='col span_2_of_3'>";
echo "<h1>".$Title."</h1>";
echo "<p class='preview'>".$Description."</p>";
echo "<a href='bloglayout.php' class='orange readmore'>Read More...</a>";
echo "</div>";
echo "</div>";
echo "</div>";
}

Limiting column for fetched data in php

I am trying to put extracted mysql data into a table using php. The issue is that I am able to put the fetched data into either all rows or all columns. What I would like to do is put the fetched data into a column format and as soon as 4 columns are used, the next data should start on the next row and so on.
Currently I am using the logic below, however I am getting everything in a column.
The goal is to limit the data to a page size so the data can be printed.
<div>
$result = mysql_query("SELECT * FROM master_data");
echo "<table border='1'>
<tr>
<th>Accounts</th>
</tr>";
echo "<tr>";
while($row = mysql_fetch_array($result))
{
echo "<td>";
echo "First Name:" . $row['First_Name'] . "</br>";
echo "Middle Name:" . $row['Middle_Name'] . "</br>";
echo "Last Name:" . $row['Last_Name'] . "</br>";
echo "</td>";
}
echo "</tr>";
echo "</table>";
mysql_close($con);
?></div>
If I understand your problem correctly you will want to do something like:
Keep a counter to see which number record you're looking at ($i)
$i = 0; // Your counter
while($row = mysql_fetch_array($result))
{
// ...
On every 4th iteration of the loop output something like </tr><tr>.
if ($i % 4 == 0 && $i > 0) // Will return true if `$i` is a multiple of 4 and greater than 0
{
echo "</tr><tr>"; // Output your HTML to start a new row
}
One final note: As others will be pointing out. You should avoid using the MySQL extension. You should use MySQLi or PDO instead.
<div>
$result = mysql_query("SELECT * FROM master_data");
echo "<table border='1'>
<tr>
<th>Accounts</th>
</tr>";
echo "<tr>";
$i = 1;
while($row = mysql_fetch_array($result))
{
if($i == 4)
{
echo "<td>";
echo "First Name:" . $row['First_Name'] . "</br>";
echo "Middle Name:" . $row['Middle_Name'] . "</br>";
echo "Last Name:" . $row['Last_Name'] . "</br>";
echo "</td>";
$i=0;
}
$i++;
}
echo "</tr>";
echo "</table>";
mysql_close($con);
?>
</div>

Echo first result from MYSQL differently than all the other results

I have a mysql table in which contains video titles, video embed html, video description, and video thumbnails. I want it to output the first entry as;
$result = mysqli_query($con,"SELECT * FROM entries");
while($row = mysqli_fetch_array($result)) {
echo $row['title'];
echo $row['html'];
echo $row['desc'];
}
and after that, I would like it to output the next five entries as
$result = mysqli_query($con,"SELECT * FROM entries");
while($row = mysqli_fetch_array($result)) {
echo $row['title'];
echo "<a href='". $row['id'] . "'><img src='" . $row['thu'] . "'></a>";
}
however I have no idea how to do this. I am kind of a newb when it comes to mysql. Any help would be appreciated.
I have looked at other similar questions but none of them really fit the bill.
Poor man's solution would be something like:
$result = mysqli_query($con,"SELECT * FROM entries");
$first = true;
while($row = mysqli_fetch_array($result)) {
if ($first) {
echo $row['title'], $row['html'], $row['desc'];
$first = false;
continue 2;
}
echo $row['title'], "<a href='{$row[id]}'><img src='{$row[thu]}'></a>";
}
(As explained in PHP chatroom)
Since mysqli_fetch_array() gets you a row, you can use it before the while() without a problem like this
$result = mysqli_query($con,"SELECT * FROM entries");
$row = mysqli_fetch_array($result);
echo $row['title'];
//etc.
while($row = mysqli_fetch_array($result)) {
echo $row['title'];
echo "<a href='". $row['id'] . "'><img src='" . $row['thu'] . "'></a>";
}
Track it with a sentinel value
$i = 0;
while($row = mysqli_fetch_array($result)) {
if($i == 0){
//first iteration
echo $row['title'];
echo $row['html'];
echo $row['desc'];
}else{
echo $row['title'];
echo "<a href='". $row['id'] . "'><img src='" . $row['thu'] . "'></a>";
}
$i++;//increment value
}

How do I create a nested list inside of a while loop?

I'm having trouble trying to get a nested < ul > inside of a while loop. I'm not sure if this even possible so i'm open to alternatives.
Here's a quick image of what my database looks like and what i'm trying to achieve.
Here's my sql
SELECT *
FROM drinks_category, drinks_lookup, drinks
WHERE drinks.drink_id = drinks_lookup.drink_id
AND drinks_lookup.drinks_category_id = drinks_category.drinks_category_id
ORDER BY drinks_category.drinks_category_title
Here's my output php
$result = $conn->query($sql) or die(mysqli_error());
$last_category = 0;
while ($row = $result->fetch_assoc()) {
if($row['drinks_category_id'] != $last_category) {
echo "<h1>" . $row['drinks_category_title'] . "</h1>";
}
echo "<p>" . $row['drink_name'] . "</p>";
$last_category = $row['drinks_category_id'];
}
Im using mysqli and php. Thanks in advance!
Update your while loop to the following:
while ($row = $result->fetch_assoc()) {
if($row['drinks_category_id'] != $last_category) {
if($last_category != 0) echo '</ul>';
echo "<h1>" . $row['drinks_category_title'] . "</h1>";
echo "<ul>";
}
echo "<li>" . $row['drink_name'] . "</li>";
$last_category = $row['drinks_category_id'];
}
if($last_category != 0) echo "</ul>";
Instead of having 3 tables, you could just add a category_id to your drinks table, just to simplify things. Then you can do this:
SELECT drink_name, (SELECT drnk_category_title FROM drinks_category WHERE drink_category_id = drink_category // THE COLUMN I SUGGESTED //) AS title FROM drinks
And then you can loop the result and build the nodes you desire really easily
$result = $conn->query($sql) or die(mysqli_error());
$last_category = 0;
while ($row = $result->fetch_assoc()) {
if($row['drinks_category_id'] != $last_category) {
// close previous </ul>
if( $last_category != 0 ) echo "</ul>";
// new title
echo "<h1>" . $row['drinks_category_title'] . "</h1>";
// new <ul>
echo "<ul>";
$last_category = $row['drinks_category_id'];
}
echo "<li>" . $row['drink_name'] . "</li>";
}
// close last </ul>
if( $last_category != 0 ) echo "</ul>";

Categories