Repeat div width with Different Database Table ID - php

I am trying to repeat a div the same div multiple times with as many rows as i have in the database table. a non working example i made of what i want is below.
<?php for ($i=0; $i < 3; $i++) {
?><!-- switch the three with the number of rows in the table-->
I would want to loop the container and switch the database query with the value of i.
<div id = "bodyContainer">
<form class="review">
<div class="reviewContainer">
<div class = "images">
<?php
$link = mysqli_connect("localhost", "root", "root","DJP");
if (mysqli_connect_error()) {
die("Could not connect to database");
}
$query = "SELECT * FROM reviews WHERE id = '<?php echo $i ?><!--switch the value of i here-->'";
$result=mysqli_query($link, $query);
$row = mysqli_fetch_array($result);
echo '<img height = 90% width = 100% src="data:image/jpeg;base64,'.$row[3].' " />';
?>
</div>
<!--<div class="title">
<?php
//echo $row[1];
?>
</div>-->
<div class = "comment">
<?php
echo $row[2];
?>
</div>
<div class = "rating" style = "float: right;">
<?php
echo $row[4];
?>
<br/>
<br/>
stars
</div>
</div><!--end review-->
</form>
</div><!--end body container-->
<?php;}?><!-- end the for loop so all the divs can be re-done with a new entry for the query. I would move the $link out of loop also.-->

Just fetch all the rows once and then go through them as you print the data:
$link = mysqli_connect("localhost", "root", "root","DJP");
if (mysqli_connect_error()) {
die("Could not connect to database");
}
$query = "SELECT * FROM reviews";
$result=mysqli_query($link, $query);
while($row = mysqli_fetch_array($result)){ ?>
<div id = "bodyContainer">
<form class="review">
<div class="reviewContainer">
<div class = "images">
<?php
echo '<img height = 90% width = 100% src="data:image/jpeg;base64,'.$row[3].' " />';
?>
</div>
<!--<div class="title">
<?php
//echo $row[1];
?>
</div>-->
<div class = "comment">
<?php
echo $row[2];
?>
</div>
<div class = "rating" style = "float: right;">
<?php
echo $row[4];
?>
<br/>
<br/>
stars
</div>
</div><!--end review-->
</form>
</div><!--end body container-->
<?php } ?>

Related

How to echo values in HTML that are coming from mysqli_multi_query in php

I am able to get values displayed from database when using mysqli_multi_query.But I am not able to print in my HTML code.Its Probably the issue with while loop. Below is my code which gives me the AVG for the three SQL statements. Only problem is with displaying average value in HTML.
<?php
$con = new mysqli("localhost", "root","","survey");
if ($con){ printf(""); }
$query = "SELECT AVG(workarea) FROM score;" ;
$query .= "SELECT AVG(manager) FROM score;";
$query .= "SELECT AVG(comm) FROM score;";
// Execute multi query
if (mysqli_multi_query($con,$query)) {
do {
if ($result=mysqli_store_result($con)) {
// Fetch one and one row
while ($row=mysqli_fetch_row($result)) {
printf("%s\n",$row[0]);
}
// Free result set
mysqli_free_result($result);
}
} while (mysqli_next_result($con));
}
?>
<div class="row count">
WORK AREA/UNIT SCORE : <?php echo $row[0]; ?> // echo $row[AVG(workarea)] doesnot work
</div>
<div class="row count">
SUPERVISOR/MANAGER SCORE : <?php echo $row[0]; ?> // echo $row[AVG(manager)]
</div>
<div class="row count">
COMMUNICATION SCORE : <?php echo $row[0]; ?> // echo $row[AVG(comm)] doesnot work
</div>
No need of multi query you just get AVG in single query. And write your html inside while loop as
$con = new mysqli("localhost", "root", "", "survey");
if ($con) {
printf("");
}
$query = "SELECT AVG(workarea),AVG(manager),AVG(comm) FROM score;";
// Execute multi query
if ($result = $con->query($query)) {
/* fetch object array */
while ($row = $result->fetch_row()) {
?>
<div class="row count">
WORK AREA/UNIT SCORE : <?php echo $row[0]; ?>
</div>
<div class="row count">
SUPERVISOR/MANAGER SCORE : <?php echo $row[1]; ?>
</div>
<div class="row count">
COMMUNICATION SCORE : <?php echo $row[2]; ?>
</div>
<?php
}
/* free result set */
$result->close();
}

Dynamically populating a div with data from a MySQL table

Lets say I have a persons table :
forename surname age gender
--------------------------------------------
adam example 90 male
john example 90 male
If I wanted to display this information in separate divs, how could this be done? Say for example the following HTML.
<div class = "container">
<div class="wrapper">
<div class = "jumbotron">
<!-- adams data here -->
</div>
<div class = "jumbotron">
<!-- johns data here -->
</div>
</div>
</div>
I'm aware on how to query the DB to get the information into PHP variables, I'm just not sure how to dynamically display the data in separate divs.
Below is how I am getting the data
<?php
if($result = $db->query("SELECT forename,surname FROM users ")){
if($count = $result->num_rows){
while($row = $result->fetch_object()){
echo $row->forename, '<br><br>';
echo $row->surname, '<br><br>';
}
$result->free();
}
}
?>
Just wrap the div inside a loop so you'll print a div for each result row
<?php foreach($result as $r): ?>
<div class = "jumbotron">
<?php echo $r['name'] // Print fields you need ?>
</div>
<?php endforeach; ?>
EDIT: Now I can see your query. Try this:
<?php
if($result = $db->query("SELECT forename,surname FROM users ")){
if($count = $result->num_rows){
while($row = $result->fetch_object()){
?>
<div class = "jumbotron">
<?php echo $row->forename; ?><br><br>
<?php echo $row->surname; ?><br><br>
</div>
<?php
}
$result->free();
}
}
?>
Well Joe, This is a fairly simple problem that has probably been answered before.
I'll use the mysqli class of php
first create a mysqli connection object in a file for best practice then include it in your main script.
<?php
$connection = new mysqli($host_address, $username, $password, $database);
?>
include this in your main script
<?php require "path\to\connection\script"; ?>
<?php
$sql_adam = "SELECT * FROM persons WHERE forename = 'adam'";
$sql_john = "SELECT * FROM persons WHERE forename = 'john'";
$qry1 = $connection->query($sql_adam);
$qry2 = $connection->query($sql_john);
?>
<div class = "container">
<div class="wrapper">
<div class = "jumbotron">
<?php if ($qry1->num_rows >= 1){
while($adam = $qry1->fetch_assoc()){
foreach ($adam as $column => $data) {
echo "<p>$column : $data </p>";
}
}
}
</div>
<div class = "jumbotron">
<?php if ($qry2->num_rows >= 1){
while($john = $qry2->fetch_assoc()){
foreach ($john as $column => $data) {
echo "<p>$column : $data </p>";
}
}
}
</div>
</div>
</div>
Your Result should be something like
<div class = "container">
<div class="wrapper">
<div class = "jumbotron">
<p>forename : adam</p>
<p>surname : example</p>
<p>age : 90</p>
<p>gender : male</p>
</div>
<div class = "jumbotron">
<p>forename : john</p>
<p>surname : example</p>
<p>age : 90</p>
<p>gender : male</p>
</div>
</div>
</div>

div inside while loop in php

The search results should display like this
But my results are stacking on top of each.
Here is my code :
<div class="container">
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("thesis") or die(mysql_error());
$search = trim( $_POST['SearchKeywords']);
$query = " SELECT * FROM new_data WHERE Product_Title or Product_link LIKE'%$search%' ";
$sql = mysql_query($query) or die(mysql_error());
$count = mysql_num_rows($sql);
$count == 0;
if ($count == 0) {
echo "Sorry, Nothing Found !!";
}else{
while ($row = mysql_fetch_array($sql))
{
$img = $row ['Product_Img'];
$link = $row ['Product_link'];
$title = $row ['Product_Title'];
$price = $row ['Product_Price'];
?>
<div class="card">
<div class="front alert alert-success">
<?php echo "$title";
echo "<img src='$img' width='80px' height='100px'>";
echo "$price"; ?>
</div>
</div>
<?php
};
};
?>
</div> <!-- Container -->
Those div blocks are inside a container.
I added a bootstrap class in order for better a design.
You can use thumbnails with custom content
<div class="row">
<div class="col-sm-6 col-md-4">
<div class="thumbnail">
<img src="..." alt="...">
<div class="caption">
<h3>Thumbnail label</h3>
<p>...</p>
<p>Button Button</p>
</div>
</div>
</div>
</div>
I used a counter inside while loop.
Which will check, when there are already 4 blocks/ products in a single row then it will create a new row
<?php
if($productCount == 0)
{
echo "<div class='row'>"; }
}
$productCount++;
if($productCount==4)
{
echo "</div>" ;
$productCount = 0;
}
?>

when no rows retrieved from SQL database then dont display a div

i tried the empty selector in jquery but it doesnt work. the content is still being displayed. i am retrieving some rows from the SQL database.. if there is no database then i dont want to display that div.
<div id="scrollingText">
<div class="scrollWrapper">
<div class="scrollableArea">
<marquee behavior="scroll" direction="left">
<p>
<?php
$con = mysql_connect("localhost","fraptech_test","");
mysql_select_db("fraptech_test", $con);
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysql_select_db("fraptech_ndsnotice", $con);
$result = mysql_query("SELECT * FROM ndsnotice");
while($row = mysql_fetch_array($result))
{
echo $row['Notice'];
}
?>
</p>
</marquee>
</div>
</div>
</div>
Is that your whole code? Are you working with ajax? If you're using pure PHP without ajax just set the output div into your "if"-conditions?
If you want to use jQuery, try:
if ( ($("div.scrollableArea p").text()).length > 0 )
{
$("div.scrollableArea p").show();
}
else
{
$("div.scrollableArea p").hide();
}
But you can do it without jQuery, I guess.
You need to put the HTML inside your php if clause.
Simple example:
<div id="scrollingText">
<?php
$con = mysql_connect("localhost","fraptech_test","");
mysql_select_db("fraptech_ndsnotice", $con);
$result = mysql_query("SELECT * FROM ndsnotice");
if (mysqli_connect_errno($con)) {
<div class="scrollWrapper">
<div class="scrollableArea">
<marquee behavior="scroll" direction="left">
<p>
<?php
while($row = mysql_fetch_array($result))
{
echo $row['Notice'];
}
?>
</p>
</marquee>
</div>
</div>
<?php } else { ?>
<div class="errordiv">Display this on error</div>
<?php } ?>
</div>
thank you guys with ur help i made some change. the below code did it:
<?php
$con = mysql_connect("localhost","fraptech_test","");
mysql_select_db("fraptech_test", $con);
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysql_select_db("fraptech_ndsnotice", $con);
$result = mysql_query("SELECT * FROM ndsnotice");
if (mysql_num_rows($result) > 0)
{
?><div id="scrollingText">
<div class="scrollWrapper">
<div class="scrollableArea">
<marquee behavior="scroll" direction="left">
<p>
<?php while($row = mysql_fetch_array($result))
{
echo $row['Notice'];
}
?> </p>
</marquee>
</div>
</div>
</div>
<?php } ?>

While Statement and Divs

I am trying to get the code below to loop using a while statement. The problem I am having is getting the divs to worth with in the PHP. Any ideas on how I can make this happen? My database consists of a columns called month, day, description, and location.
<div class="events-module">
<?php
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("sandbox", $con);
$result = mysql_query("SELECT * FROM events"); ?>
<?php while($row = mysql_fetch_array($result)) ?>
<div class="date">
<div id="month"><h4><?php echo $row['month']; ?></h4></div>
<div id="day"><h3><?php echo $row['day']; ?></h3></div>
</div>
<div class="event">
<div id="description"><p><?php echo $row['description']; ?></p></div>
<div id="location"><p><b><?php echo $row['location']; ?></b></p></div>
</div>
<?php endwhile;
mysql_close($con);
?>
</div>
You didn't say exactly what's wrong. But, it seems you are missing your start block for your while loop
while (.... ) :
^^^
... // div stuff
end while

Categories