How to random the choices in php - php

I am creating a random generated quiz with 10 questions. In generating the random question is fine, but I want to display also the choices in random, same with the questions.
This is my code that I am currently working with:
<?php
generate();
function generate(){
include('connection.php');
mysql_select_db('exam');
$result=mysql_query("SELECT * FROM questionaires
INNER JOIN choices ON questionaires.q_id=choices.q_id
WHERE RAND()<(SELECT ((10/COUNT(*))*10) FROM questionaires)
ORDER BY RAND() LIMIT 10");
$c=0;
echo "<table border='3' align='center' bordercolor='#CCCCCC'>
<tr>
<th>Number:</th>
<th>Question</th>
</tr>
";
while($row = mysql_fetch_array($result)){
$c++;
echo "<tr>";
echo "<td>" . $c . "</td>";
echo "<td>";
echo $row['question'] . "<br>";
echo "<input type='radio' name='ans'>".$row['choice_a']."</input><br>";
echo "<input type='radio' name='ans'>".$row['choice_b']."</input><br>";
echo "<input type='radio' name='ans'>".$row['choice_c']."</input><br>";
echo "<input type='radio' name='ans'>".$row['choice_d']."</input><br>";
echo "</td>";
echo "</tr>";
//}
//}
}
echo "</table>";
}
?>
Little help will highly appreciated.

You could change script to this:
echo "<td>";
echo $row['question'] . "<br>";
$ans=array($row['choice_a'],$row['choice_b'],$row['choice_c'],$row['choice_d']);
shuffle($ans);
foreach ($ans as $choice) {
echo "<input type='radio' name='ans'>".$choice."</input><br>";
} unset($choice);
echo "</td>";

Related

Getting database data into a html table

I need to get the data of the database to an HTML table.
There were similar questions asked before but none of them were similar to my requirement.
I need to get the first row of the database to the first row of the table.
So I did this.
echo "<table id='main-table' border='1px solid'> <thead> ";
echo "<tr>";
echo "<th>Board No</th>";
echo "<th>Number</th>";
echo "</tr></thead><tbody>";
while($query->fetch()){
echo "<form action='' class='' method='post'><tr>";
line 55 --> echo "<td>" . Board Code:".$row["board_code"] . Number:".$row["status"] . "</td>";
echo "</tr> </form>";
}//End of While
echo "</tbody></table>";
There is a syntax error in line 55 as mentioned above in the code.
Check this code:
echo "<table id='main-table' border='1px solid'> <thead> ";
echo "<tr>";
echo "<th>Board No</th>";
echo "<th>Number</th>";
echo "</tr></thead><tbody>";
while($query->fetch()){
echo "<form action='' class='' method='post'><tr>";
echo "<td> Board Code:".$row["board_code"]. "</td>";
echo "<td>Number:".$row["status"]. "</td>";
echo "</tr> </form>";
}
echo "</tbody></table>";
"<td>" . Board Code:".$row["board_code"] ." Number:".$row["status"] . "</td>";
edit your code like if it does not work please share your whole code will help you for sure
Check with this code
echo "<form action='' class='' method='post'><tr>";
line 55 --> echo "<td>" . Board Code:".$row["board_code"] ." Number:".$row["status"] . "</td>";
echo "</tr> </form>";

while loop only returning 1 product row

Evening all,
I have the following script setup on the backend of an eCommerce platform I am developing. Essentially I want to pull the products through and also pull all of the current categories through so that products can be assigned to a particular category.
However my product edit page is only displaying 1 result.
Its interesting to note that if i remove the script relating to the last SQL query my products show but with no category options. With the script only 1 product shows with the correct categories.
<?php
$myusername= $_SESSION['login_user'];
include '../ecommerce/connection.php';
$sql="SELECT * FROM products where status='yes' ";
// Posting Result
$result = mysqli_query($connection, $sql);
// Counting Results
$count=mysqli_num_rows($result);
if($count==0) {
echo "<div class='no_order_info_box'><br><h1 id='order_h1'>No products setup</h1></div>";
} else {
if ($result = mysqli_query($connection, $sql)){
echo "<table id='order_table_small'>";
echo "<th>Image</th>";
echo "<th>Product ID</th>";
echo "<th>Name</th>";
echo "<th>Description</th>";
echo "<th>Price</th>";
echo "<th>Weight</th>";
echo "<th>Options </th>";
while($row = $result->fetch_array())
{
$product_no = $row['product_id'];
echo "<tr>";
echo '<td><img src="images/'. $row['image'] . ' " id="product_image_admin">';
echo "</td> ";
echo "<td>" .$row['product_id'];
echo "</td> ";
echo "<td>" .$row['product_name'];
echo "</td> ";
echo "<td>" .$row['product_description'];
echo "</td> ";
echo "<td>" .'&pound'.$row['product_price'];
echo "</td> ";
echo "<td>" .$row['product_weight'].'kg';
echo "</td> ";
echo "<td><form action='store_configuration/edit_product' method='post' id='delivery_change_form'>";
echo "<input type='text' name='product' value='$product_no' style='opacity: 0;'/>";
echo "<input type='Submit' value='Edit Product' >";
echo "</form></td>";
echo "<td><form action='store_configuration/change_parent_category' method='post' id='delivery_change_form'>";
echo "Parent Category <select name='category' style='height: auto;' >";
**include '../ecommerce/connection.php';
$sql="SELECT category_name FROM categories where status='yes' ";
// Posting Result
$result = mysqli_query($connection, $sql);
// Counting Results
$count=mysqli_num_rows($result);
if($count==0) {
echo "<div class='no_order_info_box'><br><h1 id='order_h1'>No categories setup</h1></div>";
} else {
if ($result = mysqli_query($connection, $sql)){
while($row = $result->fetch_array()) {
$category = $row['category_name'];
echo" <option value='$category' >".$category ."</option>";
}
}
}**
echo "</select>";
echo "<br><br><input type='Submit' value='Update Category' >";
echo "</form></td>";
echo "</tr>";
}
echo "</table>";
}
}
?>
I would love any help on how I can show all products with all categories.
I have tried everything I can think of.
Thanks.
Stan.
You've used the same names of variables for products and categories queries. While the categories are inside of products loop you reassign that variable with new query and that's why it's crashes. Change name of variables for second query and that should fix your script.
<?php
$myusername= $_SESSION['login_user'];
include '../ecommerce/connection.php';
$sql="SELECT * FROM products where status='yes' ";
// Posting Result
$result = mysqli_query($connection, $sql);
// Counting Results
$count=mysqli_num_rows($result);
if($count==0) {
echo "<div class='no_order_info_box'><br><h1 id='order_h1'>No products setup</h1></div>";
} else {
if ($result = mysqli_query($connection, $sql)){
echo "<table id='order_table_small'>";
echo "<th>Image</th>";
echo "<th>Product ID</th>";
echo "<th>Name</th>";
echo "<th>Description</th>";
echo "<th>Price</th>";
echo "<th>Weight</th>";
echo "<th>Options </th>";
while($row = $result->fetch_array())
{
$product_no = $row['product_id'];
echo "<tr>";
echo '<td><img src="images/'. $row['image'] . ' " id="product_image_admin">';
echo "</td> ";
echo "<td>" .$row['product_id'];
echo "</td> ";
echo "<td>" .$row['product_name'];
echo "</td> ";
echo "<td>" .$row['product_description'];
echo "</td> ";
echo "<td>" .'&pound'.$row['product_price'];
echo "</td> ";
echo "<td>" .$row['product_weight'].'kg';
echo "</td> ";
echo "<td><form action='store_configuration/edit_product' method='post' id='delivery_change_form'>";
echo "<input type='text' name='product' value='$product_no' style='opacity: 0;'/>";
echo "<input type='Submit' value='Edit Product' >";
echo "</form></td>";
echo "<td><form action='store_configuration/change_parent_category' method='post' id='delivery_change_form'>";
echo "Parent Category <select name='category' style='height: auto;' >";
**include '../ecommerce/connection.php';
$sql2="SELECT category_name FROM categories where status='yes' ";
// Posting Result
$result2 = mysqli_query($connection, $sql2);
// Counting Results
$count=mysqli_num_rows($result2);
if($count==0) {
echo "<div class='no_order_info_box'><br><h1 id='order_h1'>No categories setup</h1></div>";
} else {
if ($result2 = mysqli_query($connection, $sql2)){
while($row2 = $result2->fetch_array()) {
$category = $row2['category_name'];
echo" <option value='$category' >".$category ."</option>";
}
}
}**
echo "</select>";
echo "<br><br><input type='Submit' value='Update Category' >";
echo "</form></td>";
echo "</tr>";
}
echo "</table>";
}
}
?>

Update sql table of only rows where checkbox is checked php

I have a table of values that I am trying to update only the date of. I have a checkbox that I want only the select rows to updated when its checked and user clicks the submit button. The issue I am having is let's say I have 4 records and I only want record 1,2 and 4 updated. What is happening is records 1,2 and 3 are being updated. I know it has something to do with the array in the foreach loop. Side note I know the sql code allows for injection I am trying to get this to work first before worrying about that. Thank you for any and all help and time.
Table Code
$sql = "SELECT * FROM tblUnitMaster WHERE JobNumber = '" . $JobNumber . "' AND `EngReleaseDate` IS NULL OR `EngReleaseDate` = '' ORDER BY UnitID";
$result=$conn->query($sql);
echo "<style>";
echo "th {";
echo "background-color: #6EB4FF;";
echo "color: white;";
echo "}";
echo "</style>";
echo "<form action=pmReleaseEngineering3.php method=post>";
echo "<input type=hidden name=JobNumber value=$JobNumber />";
echo "<table border=2 cellpadding=4 cellspacing=10>";
echo "<tr>";
echo "<th><b><font size=4></font></b></th>";
echo "<th><b><font size=4>"."Job"."</font></b></th>";
echo "<th><b><font size=4>"."Code"."</font></b></th>";
echo "<th><b><font size=4>"."Model Number"."</font><?b></th>";
echo "<th><b><font size=4>"."Serial Number"."</font><?b></th>";
echo "<th><b><font size=4>"."Scope"."</font></b></th>";
echo "<th><b><font size=4>"."Date"."</font></b></th>";
echo "</tr>";
while ($row=$result->fetch_array())
{
echo "<tr>";
//echo "<td><font color=silver size=4>".$row[0]."</font></td>"; // Unit Number
echo "<td><input type=checkbox name=check[] value=".$row[0]." checked></td>";
echo "<td><input name=UnitID type=text font color=silver size=3 value=".$row[0]." readonly/></td>"; // Unit Number
echo "<td><font color=silver size=4>".$row[5]."</font></td>"; // Job Code
echo "<td><font color=silver size=4>".$row[2]."</font></td>"; // Model Number
echo "<td><font color=silver size=4>".$row[3]."</font></td>"; // Serial Number
echo "<td><font color=silver size=4>".$row[4]."</font></td>"; // Scope
echo "<td><input name=EngReleaseDate size=6 type=datetime value=" . date('Y-m-d'). " /></td>";
echo "</tr>";
}
echo "</table>";
echo "<br>";
echo "<button class=button style=vertical-align:middle><span>Submit</span></button>";
echo "</form>";
echo "<br>";
PHP SQL Update TBL Code
include('pmconnect.php');
$JobNumber=$_REQUEST['JobNumber']; //array
$UnitID=$_POST['UnitID']; // array
$EngReleaseDate=$_POST['EngReleaseDate']; // array
if(isset($_POST['check'])){
//if(!empty($_POST['check'])) {
foreach($_POST['check'] as $a => $B){
$sql = "UPDATE tblUnitMaster SET " .
"EngReleaseDate='" . $EngReleaseDate . "'" .
"WHERE UnitID='" . $UnitID . "'";
//$result=$conn->query($sql);
if ($conn->multi_query($sql) === TRUE)
echo "<table border=1 cellpadding=4 cellspacing=10>";
echo "<tbody>";
echo "<tr>";
echo "<td><b><font size=4>Unit ID</font></b></td>";
echo "<td><b><font size=4>Eng. Release</font></b></td>";
echo "</tr>";
echo "<tr>";
echo "<td><font size=4> $UnitID </font></td>";
echo "<td><font size=4> $EngReleaseDate </font></td>";
echo "</tr>";
echo "</tbody>";
echo "</table>";
echo"<br>";
}
}

php mysql how to join two table

I have these codes:
<?php
$records = mysql_connect('localhost', 'root', '') or die(mysql_error());
mysql_select_db('records', $records);
if(isset($_GET['src']))
{
$sql = "SELECT * FROM students where studentnumber like '%{$_GET['src']}%'";
$cnt = mysql_num_rows(mysql_query($sql));
if ($cnt == 0)
{
echo "<script>alert('No Record Found');</script>";
}
$result = mysql_query($sql, $records);
echo "<table border='0' class='table table-striped table-bordered table-hover'>";
echo "<tr class='info'><td width='10%'>Name</td><td width='11%'>Course Yr-Sec</td><td width='10%'>Student Number</td><td width='10%'>Violation</td><td width='10%'>Punishment</td><td width='9%'>Violation Date</td><td width='7%'>Punishment Date</td><td width='5%'>CS Length</td><td width='4%'>CS Done</td><td width='4%'>CS Left</td><td width='17%'><center>Action</center></td></tr></tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>";
echo $row['Lastname'];
echo ", ";
echo $row['Firstname'];
echo " ";
echo $row['Middleinitial'];
echo "</td>";
echo "<td>";
echo $row['Course'];
echo " ";
echo $row['Year'];
echo "-";
echo $row['Section'];
echo "</td>";
echo "<td>";
echo $row['Studentnumber'];
echo "</td>";
echo "<td>";
echo $row['Violation'];
echo "</td>";
echo "<td>";
echo $row['Punishment'];
echo "</td>";
echo "<td>";
echo $row['Violationdate'];
echo "</td>";
echo "<td>";
echo $row['Punishmentstartdate'];
echo "</td>";
echo "<td>";
echo $row['CSlength'];
echo "</td>";
echo "<td>";
echo $row['CSDone'];
echo "</td>";
echo "<td>";
echo $row['CSLeft'];
echo "</td>";
echo "<td>";
echo "<a href='edit.php?no={$row['ID']}'><input type='button' name='edit' value='Edit' class='btn btn-success'></a>";
echo " <a href='delete.php?no={$row['ID']}'><input type='button' name='delete' value='Delete' class='btn btn-danger'></a>";
echo " <input type='button' name='view' value='View' class='btn btn-info'>";echo "</td>";
echo "</tr>";
}
echo "</table>";
}
else
{
$sql = 'SELECT * FROM students';
$result = mysql_query($sql, $records);
echo "<table border='0' class='table table-striped table-bordered table-hover'>";
echo "<tr class='info'><td width='10%'>Name</td><td width='11%'>Course Yr-Sec</td><td width='10%'>Student Number</td><td width='10%'>Violation</td><td width='10%'>Punishment</td><td width='9%'>Violation Date</td><td width='7%'>Punishment Date</td><td width='5%'>CS Length</td><td width='4%'>CS Done</td><td width='4%'>CS Left</td><td width='17%'><center>Action</center></td></tr></tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>";
echo $row['Lastname'];
echo ", ";
echo $row['Firstname'];
echo " ";
echo $row['Middleinitial'];
echo "</td>";
echo "<td>";
echo $row['Course'];
echo " ";
echo $row['Year'];
echo "-";
echo $row['Section'];
echo "</td>";
echo "<td>";
echo $row['Studentnumber'];
echo "</td>";
echo "<td>";
echo $row['Violation'];
echo "</td>";
echo "<td>";
echo $row['Punishment'];
echo "</td>";
echo "<td>";
echo $row['Violationdate'];
echo "</td>";
echo "<td>";
echo $row['Punishmentstartdate'];
echo "</td>";
echo "<td>";
echo $row['CSlength'];
echo "</td>";
echo "<td>";
echo $row['CSDone'];
echo "</td>";
echo "<td>";
echo $row['CSLeft'];
echo "</td>";
echo "<td>";
echo "<a href='edit.php?no={$row['ID']}'><input type='button' name='edit' value='Edit' class='btn btn-success'></a>";
echo " <a href='delete.php?no={$row['ID']}'><input type='button' name='delete' value='Delete' class='btn btn-danger'></a>";
echo " <input type='button' name='view' value='View' class='btn btn-info'>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
?>
It contains search, edit, delete and view functions...now my question is...I wanted to join the two tables in the database by the column studentnumber...
my table students contains the column Lastname, Firstname, Middleinitial, Course, Year, Section, Studentnumber, Violation, Punishment, Violationdate, Punishmentstartdate, CSlength, ID, CSDone, CSLeft...now my another table named students2 contains the following rows ID, Studentnumber, Violation, Punishment, Violationdate, Punishmentstartdate, CSlength, CSDone, CSLeft...I want to display the information from my both tables...for example I want to view all the records from database with a studentnumber of 20101000...do I have to inner join the tables?
I'm just a newbie in php...
Thank you in advance... :)
This is a LEFT JOIN. It will return all of the records from students1, as well as any records from students2 where the record has the same studentnumber as a record in students1:
SELECT * FROM students1
LEFT JOIN students2
ON students1.studentnumber = students2.studentnumber
AND students1.studentnumber = 20101000
An INNER JOIN returns only records that produce a match, so you will only get records where there is an identical studentnumber in both students1 and students2. Based on your comments, I believe this is the style you are looking for:
SELECT * FROM students1
INNER JOIN students2
ON students1.studentnumber = students2.studentnumber
AND students1.studentnumber = 20101000
If you want to get your head around using JOINs, I'd recommend trying both of these statements to observe the results. Then try a few other approaches, perhaps using this excellent tutorial on the Coding Horror Blog.

PHP postback on submit

I have a question on php postback
My code is :
<?php
if(isset($_POST["Delete"]))
{
echo "DELETE";
}
if(isset($_POST["Modifier"]))
{
echo "Modifier";
}
if(!empty($_SESSION["Status"]))
{
if($_SESSION["Status"] == "u")
{
header("Location: Index.php?Action=Acceuil");
}
if($_SESSION["Status"] == "a")
{
$Connection = mysql_connect("localhost","root") or die(mysql_error());
mysql_select_db("tpw34") or die("Nope.");
$query = "Select * From Products";
$result = mysql_query($query);
While($ligne = mysql_fetch_assoc($result))
{
//Index.php?Action=AdminDeleteProduct&Delete=".$ligne["ProductID"]."
echo "<form method='POST' Action='#'>";
echo "<table border='1'>";
echo "<tr>";
echo "<td colspan='2'><center><img width='250' height='250' src='".$ligne["Image"]."'/></center></td>";
echo "</tr>";
echo "<tr>";
echo "<th>Nom du produit :</th>";
echo "<td>".$ligne["ProductName"]."</td>";
echo "</tr>";
echo "<tr>";
echo "<th>Prix :</th>";
echo "<td>".$ligne["Prix"]."</td>";
echo "</tr>";
echo "<tr>";
echo "<th>Description :</th>";
echo "<td>".$ligne["Description"]."</td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='2'h><input type='Submit' value='Delete' name='Delete'/><input type='Submit' value='Modifier' name='Modifier'/></td>";
echo "</tr>";
echo "</table>";
echo "<br>";
echo "</form>";
}
}
}
?>
My Question is : I want to get the ProductID of the item ( in the table ) to be in the $_POST["Delete"] and $_POST["Modifier"] but i dont wanna change the text on the button. I want to keep DELETE and MODIFIER. I have read many things on the web but i dont find the correct answer.
Include a hidden form value for ProductID. Then you can retrieve the value in $_POST['ProductID']
echo "<input type=hidden name='ProductID' value='" . $ligne["ProductID"] . "'>";
You could work with sessions, where you can temporarily save your information.
Sessions
Or like Tim Dearborn suggested, use a hidden input to send it with the next form submit.

Categories