Display 3 items per row - while loop - php/mysql [duplicate] - php

This question already has answers here:
display only 3 foreach result per row
(2 answers)
Closed 1 year ago.
I am currently working on a loop to display items from a mysql table. Is there a simple way to display 3 items per row. So far I managed to display all the items in a single row inside an html table . I would appreciate any help;
code (without html table tags) below:
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<?php
$database_name = "vog";
$conn = mysql_connect("localhost","root","toor");
mysql_select_db($database_name);
$sql = "select * from client1";
$result = mysql_query($sql) or die(mysql_error());
$num = mysql_num_rows($result); //Ελεγχος αν υπάρχουν εγγραφές!
?>
<?php
if($num){
while ($row = mysql_fetch_array($result))
{
echo $img_id = $row['img_id'];
?>
<form name="add2cart" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php echo $row['img_name'];?>
<?php echo "<img src=".$row['img_path'].">";?>
<select name="color">
<option>bnw</option>
<option>sepia</option>
</select>
<input type="hidden" name="img_name" value="<?php echo $row['img_name']; ?>">
<input type="submit" name="add2cart" value="Add to cart"></input>
<br />
</form>
<?php
}
}
else{
echo "Δεν υπάρχουν εγγραφές με τα κριτήρια που επιλέξατε";
}
//add2cart section
if(isset($_POST['add2cart'])){
$img_name = $_POST['img_name'];
$color = $_POST['color'];
$sql_order ="insert into orders(item_id, img_name, color)
values(' ', '$img_name', '$color')";
$result = mysql_query($sql_order);
}
?>

Declare a variable before your loop like: $currentRow = 1 and then inside, and at the end, of your loop add $currentRow++
You can then check if your row is divisible by 3 if($currentRow % 3 == 0) and put a break in.

Related

quiz: can not get the correct answer with rand() [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Reference - What does this error mean in PHP?
(38 answers)
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 3 years ago.
i got this code from technopoints, i want to get a random question from database but i cant work this out when i put the $fetchqry = "SELECT * FROM quiz ORDER BY rand()"; it does not get the correct result i think because of the rand(). i've been trying to fix this and it doesn't work and also how can i put an incremented number per question?
if (isset($_POST['click']) || isset($_GET['start'])) {
#$_SESSION['clicks'] += 1 ;
$c = $_SESSION['clicks'];
if(isset($_POST['userans'])) { $userselected = $_POST['userans'];
$fetchqry2 = "UPDATE 'quiz' SET 'userans'='$userselected' WHERE 'id'=$c-1";
$result2 = mysqli_query($db,$fetchqry2);
}
} else {
$_SESSION['clicks'] = 0;
}
//echo($_SESSION['clicks']);
?>
<div class="bump"><br><form><?php if($_SESSION['clicks']==0){ ?> <button class="button" name="start" float="left"><span>START QUIZ</span></button> <?php } ?></form></div>
<form action="" method="post">
<table>
<?php if(isset($c)) { $fetchqry = "SELECT * FROM 'quiz' ORDER BY rand()";
$result=mysqli_query($db,$fetchqry);
$num=mysqli_num_rows($result);
$row = mysqli_fetch_array($result,MYSQLI_ASSOC); }
?>
<tr><td><h3><br><?php echo #$row['que'];?></h3></td></tr> <?php if($_SESSION['clicks'] && $_SESSION['clicks']){ ?>
<tr><td><input required type="radio" name="userans" value="<?php echo $row['option 1'];?>"> <?php echo $row['option 1']; ?><br>
<tr><td><input required type="radio" name="userans" value="<?php echo $row['option 2'];?>"> <?php echo $row['option 2'];?></td></tr>
<tr><td><input required type="radio" name="userans" value="<?php echo $row['option 3'];?>"> <?php echo $row['option 3']; ?></td></tr>
<tr><td><input required type="radio" name="userans" value="<?php echo $row['option 4'];?>"> <?php echo $row['option 4']; ?><br><br><br></td></tr>
<tr><td><button class="button3" name="click" >Next</button></td></tr> <?php }
?>
<form>
<?php if($_SESSION['clicks']){
$qry3 = "SELECT 'ans', 'userans' FROM 'quiz';";
$result3 = mysqli_query($db,$qry3);
$storeArray = Array();
while ($row3 = mysqli_fetch_array($result3, MYSQLI_ASSOC)) {
if($row3['ans']==$row3['userans']){
#$_SESSION['score'] += 1 ;
}
}
?>
<h2>Result</h2>
<span>No. of Correct Answer: <?php echo $no = #$_SESSION['score'];
session_unset(); ?></span><br>
<span>Your Score:&nbsp<?php echo $no*2; ?></span>
<?php } ?>```
I think your sql query should be like this:
$fetchqry = "SELECT * FROM quiz ORDER BY rand() LIMIT 1";
I'm not pretty sure if you have to show all the rows into quiz or only one, if its like this please try with LIMIT 1

PHP: Incrementation Error - Need assistance here [duplicate]

This question already has answers here:
increment variable each time page gets called or opened?
(7 answers)
Closed 6 years ago.
Good day i have a form which its values are grabbed from a database,
<?php
$questionId = 1;
$pdo = new PDO("mysql:host=localhost;dbname=mydb", 'root', '');
$sth = $pdo->prepare("SELECT * FROM questions WHERE questionid = $questionId");
$sth->execute();
while ($row = $sth->fetch()):
?>
<form action="exam1.php" method="POST">
<?php echo "<h4>".$row['instructions'] ."</h4><br>"; ?><h4><?php echo $questionId. ". ".$row['name']; ?></h4>
<input type="radio" name="choices" id="choices" value="<?php echo $row['choice1']; ?>"><?php echo $row['choice1']; ?> ...(other choices)
<input type="submit" name="submitAnswers">
</form>
<?php endwhile;?>
what i want is how to increment the $questionId variable once the form is being submitted. i tried this
if (isset($_GET['choices'])) {
echo $_GET['choices'];
$questionId = $questionId + 1;
}
the question id updated but just once.
i need it to always update by adding 1 to it.
<form method="post" >
<input type="submit" name="submit">
</form>
<?php
session_start();
$_SESSION['id'];
if (isset($_POST['submit'])) {
$_SESSION['id']=$_SESSION['id']+1;
}
echo "Value is => ",$_SESSION['id'],"<br>";
?>

PHP/SQL - How can I get the name of tables in my database?

I am trying to use the function mysqli_fetch_field() to get the name of each of my tables in the database. However when i try to output the table name using $fieldInfo->table i get duplicates. How can i select only 1 column from each table so that $fieldInfo->table isnt called for every column of each table?
current sql:
$sql = "SELECT * from administrators, bookings, customers, rooms";
$results = mysqli_query($conn, $sql)
or die ('Problem with query' . mysqli_error($conn));
my code to display the table name in radio buttons:
<?php
while ($fieldInfo = mysqli_fetch_field($results)) {
?>
<input type="radio" name="tableNames" value="<?php echo $fieldInfo->table; ?>"> <?php echo $fieldInfo->table ?> <br>
<?php } ?>
I added 2 temporary table name holder and made an IF condition that only outputs the radio buttons once the 2 temporary name holders are different.
<?php
$tempName2 = "";
while ($fieldInfo = mysqli_fetch_field($results)) {
$tempName = $fieldInfo->table;
if ($tempName != $tempName2) {
$tempName2 = $tempName;
?>
<input type="radio" name="tableNames" value="<?php echo $tempName; ?>" > <?php echo $tempName ?> <br>
<?php }
} ?>
<?php
$query='SHOW TABLES FROM DB_NAME';
$results=mysqli_query($conn,$query);
while ($fieldInfo = mysqli_fetch_array($results)) { ?>
<input type="radio" name="tableNames" value="<?php echo $fieldInfo[0]; ?>"> <?php echo $fieldInfo[0]; ?> <br>
<?php } ?>

Checkboxes and text doesn't want to show the value

<!DOCTYPE html>
<html>
<head>
<title>Talenquiz2</title>
</head>
<body>
<?php
if (isset($_GET["controleer"]))
{
$vraag = $_GET["vraag"];
$juistantwoord = $_GET["juistantwoord"];
$foutantwoord1 = $_GET["foutantwoord1"];
$foutantwoord2 = $_GET["foutantwoord2"];
$con = mysql_connect("localhost","root","");
mysql_select_db("dbproject", $con);
$result = mysql_query("SELECT * FROM tblquizvragen");
while($row = mysql_fetch_array($result))
{
if ($row['vraag'] == $vraag)
{
if ($row['juistantwoord'] == $juistantwoord)
{
echo "Juist!<br />";
}
else
{
echo "Fout!<br />";
}
}
}
mysql_close($con);
echo "\n<hr />\n";
}
$aantalvragen=1;
$con = mysql_connect("localhost","root","");
mysql_select_db("dbproject", $con);
$result = mysql_query("SELECT * FROM tblquizvragen WHERE id='". $aantalvragen . "';");
$row = mysql_fetch_array($result);
The program is a quiz, it asks 5 questions with 3 chckbox 1 is corect and 2 are incorrect.
for ($aantalvragen=1; $aantalvragen<=5; $aantalvragen++)
{
$row = mysql_fetch_array($result);
}
her i lin
$vraag = $row['vraag'];
$juistantwoord = $row['juistantwoord'];
$foutantwoord1 = $row['foutantwoord1'];
$foutantwoord2 = $row['foutantwoord2'];
mysql_close($con);
?>
<form>
It doensn't show the values of the rows in my browser it shows only an open text and an open checkbox.
<input type="text" name="vraag" value="<?php echo $vraag; ?>" /><br />
<input type="checkbox" name="juistantwoord" value="<?php echo $juistantwoord; ?>" /><br />
<input type="checkbox" name="foutantwoord1" value="<?php echo $foutantwoord1; ?>" /><br />
<input type="checkbox" name="foutantwoord2" value="<?php echo $foutantwoord2; ?>" /><br />
<input type="submit" value="Controleer je antwoord" name="controleer" />
</form>
</body>
</html>
Your code is incorrect for fetching from the db
for(...) {
$row = mysql_fetch_array(...);
}
You simply loop over 5 lines of results, regardless of how many there may be, and assign the row array to $row... but do so for EVERY row without ever using them. So you end up trashing the first n-1 rows and come out of the loop with only row n saved.
If you're wrong with how many rows of data you're expecting, your 5-item loop may have only a 4-item result set to deal with, and the final row $row1 value will be the boolean FALSE that msyql_fetch returns when there's no more data.
Try something like this instead:
while($row = mysql_fetch_assoc($result)) {
echo ..... your stuff here ...
}
Far more reliable, doesn't depend on there being a known number of rows available, and will not output anything if there's no data at all.

How to read/send post data with php and hold a variable in it

I have this code in a loop in my code, The loop makes one submit button for every member found. I need each button to have the members name stored in it, in a way it can be sent though post when that button is clicked. Im not sure if this is possible with post but i was trying a way i do it with URLS. Does anyone know how to do this?
<input type="submit" value="Attack" name="Attack?name=<?php echo $Member_name; ?>" />
<?php
if(isset($_POST['Attack'])){
$sql = "SELECT * FROM users WHERE name='".mysql_real_escape_string($_GET['name'])."'";
$query = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_object($query);
}
Here is the whole code i was trying to store it in a hidden form but it only grabs the last member found and wont get others.
<?php
$sql = "SELECT name, rank FROM users ORDER BY rank DESC"; // Searches the database for every one who has being last active in the last 5 minute
$query = mysql_query($sql) or die(mysql_error());
$count = mysql_num_rows($query);
$i = 1;
while($row = mysql_fetch_object($query)) {
$Member_name = htmlspecialchars($row->name);
$Member_level = htmlspecialchars($row->rank);
?>
<td><?php echo $i; ?></td>
<td><?php echo $Member_name; ?></td><td><?php echo $Member_level; ?></td><td>
<input type="hidden" name="thename" value="<?php echo $Member_name; ?>">
<input type="submit" value="Attack" name="Attack" />
</td>
<?
if($i != $count) { // this counts the amount of people that are online and display the results.
echo "</tr><tr>";
}
$i++;
}
?>
<?php
if(isset($_POST['Attack'])){
$sql = "SELECT * FROM users WHERE name='".mysql_real_escape_string($_POST['thename'])."'";
$query = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_object($query);
$profile_id = htmlspecialchars($row->id);
$profile_userip = htmlspecialchars($row->userip);
$profile_name = htmlspecialchars($row->name);
$profile_money = htmlspecialchars($row->money);
$profile_gang = htmlspecialchars($row->gang);
$profile_exp = htmlspecialchars($row->exp);
$profile_profile = htmlspecialchars($row->profile);
$profile_rank = htmlspecialchars($row->rank);
$profile_health = htmlspecialchars($row->health);
$profile_defence = htmlspecialchars($row->defence);
$profile_stanima = htmlspecialchars($row->stanima);
?>
OK, assuming everything else is working ok, and you are retrieving data.
Change this:
<input type="hidden" name="thename" value="<?php echo $Member_name; ?>">
<input type="submit" value="Attack" name="Attack" />
To this:
<form method="POST" action="">
<input type="hidden" name="name" value="<?php echo $Member_name; ?>">
<input type="submit" value="Attack" name="Attack" />
</form>
And also in your PHP, change this line:
$sql = "SELECT * FROM users WHERE name='".mysql_real_escape_string($_GET['name'])."'";
To:
$sql = "SELECT * FROM users WHERE name='".mysql_real_escape_string($_POST ['name'])."'";
This isn't the best way to do this, you will be generating loads of HTML elements depending how many users you have, but it should solve you problem (providing everything else is working and receiving data).
HTML 5 & Javascript would be perfect for this and is something you should look into.

Categories