$resultSet = mysql_query("SELECT * FROM news ORDER BY id DESC LIMIT 7");
$product_count = mysql_num_rows($resultSet);
if($product_count >0){
while( mysql_fetch_array($resultSet)){
$id= $row["id"];
$cat= $row["categories"];
$title= $row["title"];
$image= $row["image"];
$txt= $row["txt"];
$res= $row["resource"];
}
}
else
{ echo'site is under maintaince ';}
echo " <div class=\"row-fluid\">
<div class=\"span6 post no-margin-left\">
<figure>
<img src=\"$image\" alt=\"Thumbnail 1\" />
<div class=\"cat-name\">
<span class=\"base\">'$cat'</span>
<span class=\"arrow\"></span>
</div>
</figure>
<div class=\"text\">
<h2>'$title'</h2>
<p>$txt</p>
<div class=\"meta\">By ' $res' | '$date' | 15 comments</div>
</div>
</div>
</div>";
?>
The code is not showing the output results of the table. Can you tell me what I am doing wrong? I want to show the output in the div class. My paging is working correctly, but it is not showing the output correctly. Can you guide me? I will really be grateful.
while( mysql_fetch_array($resultSet)){
$id= $row["id"];
$cat= $row["categories"];
$title= $row["title"];
$image= $row["image"];
$txt= $row["txt"];
$res= $row["resource"];
}
}
Where is $row in the lopp? However, to populate all the results, you need the printing inside the loop, otherwise you won't get them iterated
Related
I have the following code. How do I show the first image in the database with index of 0 for the large image display at end of the code? Right now it is showing the last image in the database.
<div id="imgWheel" class="treatmentContainer">
<?php
$query = "SELECT * FROM images WHERE user = 0 ORDER BY id;";
$result = $mysqli->query($query);
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$product = $row["product"];
$room = $row["room"];
$style = $row["style"];
$tags = $row["tags"];
$src = $row["url"];
$dataid = $row["id"];
$imgClass = "";
if (in_array($src, $favourites)) {
$imgClass = " favourite";
}
echo "<div class='treatment$imgClass' data-url='$src' data-product='$product' data-room='$room' data-style='$style' data-tags='$tags' data-number='$dataid' id='pic_$dataid' >";
echo "<img src='$src' crossorigin='anonymous'/>";
echo "</div>";
}
?>
</div> <!-- close imgWheel -->
<!-------- Large Image Display------- -->
<div id="display">
<img id="mainImage" src="<?php echo $src ?>" />
</div>
Your result set is alreadyx ordered by id, so you need only a variable, to be filled once with the first imageurl
<div id="imgWheel" class="treatmentContainer">
<?php
$bigpictureurl = "";
$query = "SELECT * FROM images WHERE user = 0 ORDER BY id;";
$result = $mysqli->query($query);
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$product = $row["product"];
$room = $row["room"];
$style = $row["style"];
$tags = $row["tags"];
$src = $row["url"];
$dataid = $row["id"];
if (empty($bigpictureurl)) {
$bigpictureurl = $src ;
}
$imgClass = "";
if (in_array($src, $favourites)) {
$imgClass = " favourite";
}
echo "<div class='treatment$imgClass' data-url='$src' data-product='$product' data-room='$room' data-style='$style' data-tags='$tags' data-number='$dataid' id='pic_$dataid' >";
echo "<img src='$src' crossorigin='anonymous'/>";
echo "</div>";
}
?>
</div> <!-- close imgWheel -->
<!-------- Large Image Display------- -->
<div id="display">
<img id="mainImage" src="<?php echo $bigpictureurl ?>" />
</div>
You just need to update your SQL query, just add LIMIT 1. This will limit the result just to 1 record and as you have ORDER id ASC, it will show the first record of the given user (as per you it is 0).
$query = "SELECT * FROM images WHERE user = 0 ORDER BY id ASC LIMIT 1;";
A quick and dirty solution is to save your first image in some separate variables, for example like this:
$isFirst = true;
$firstImageSrc = "";
$result = ....;
while (...) {
// set your $product, $room etc here
if ($isFirst) {
$isFirst = false;
$firstImageSrc = $src;
}
}
echo ...
?>
</div> <!-- close imgWheel -->
<!-------- Large Image Display------- -->
<div id="display">
<img id="mainImage" src="<?php echo $firstImageSrc ?>" />
</div>
A much more elegant solution would be to create an array with all your images, so that you can separate your php from your html. I will refactor your code below, and fix your first image problem as well:
<?php
$images = [];
$idx = 0;
$query = "SELECT * FROM images WHERE user = 0 ORDER BY id;";
$result = $mysqli->query($query);
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$images[$idx]["product"] = $row["product"];
$images[$idx]["room"] = $row["room"];
$images[$idx]["style"] = $row["style"];
$images[$idx]["tags"] = $row["tags"];
$images[$idx]["src"] = $row["url"];
$images[$idx]["dataid"] = $row["id"];
$images[$idx]["imgClass"] = "";
if (in_array($src, $favourites)) {
$images[$idx]["imgClass"] = " favourite";
}
$idx++;
}
?>
<div id="imgWheel" class="treatmentContainer">
<?php foreach ($images as $image) { ?>
<div class='treatment<?=$image["imgClass"]?>' data-url='<?=$image["src"]?>' data-product='<?=$image["product"]?>' data-room='<?=$image["room"]?>' data-style='<?=$image["style"]?>' data-tags='<?=$image["tags"]?>' data-number='<?=$image["dataid"]?>' id='pic_<?=$image["dataid"]?>' >
<img src='<?=$image["src"]?>' crossorigin='anonymous'/>
</div>
<?php } ?>
</div> <!-- close imgWheel -->
<!-------- Large Image Display------- -->
<div id="display">
<img id="mainImage" src="<?=$images[0]["src"]?>" />
</div>
Since you have all of that in your WHILE statement, I assume you want to echo all those records. And then at the end show the 1st pic. So for the "Large Image Display," give this a try:
<div id="display">
$query = "SELECT * FROM images WHERE user = 0;";
$result = $mysqli->query($query);
$row = $result->fetch_array(MYSQLI_ASSOC)
$src = $row["url"];
<img id="mainImage" src="<?php echo $src ?>" />
</div>
If you'd like less code, then save the value of $src inside your WHILE loop when user=0 into some other variable like $src2. And then your code simply becomes:
<img id="mainImage" src="<?php echo $src2 ?>" />
I developed a simple upload photo and comment system. When I use for each, it repeats the image. E.g If there are 5 comments on a photo. It repeats the photo 5 times with the 5 different comments. Instead of one image for 5 comments.
I want 1 image with each comments. How do I get all the comments for each photo without repeating the photo?
I use PDO and here is the php code:
<?php
//This is for the images
$results = $connecDB->prepare("select image,caption,id from gallery order by id desc LIMIT $start, $limit ");
$results->execute();
$results = $results->fetchAll();
foreach($results as $results) {
$pid=$results["id"];
//This is for the comments
$re= $connecDB->prepare("select * from comments where pid = :pid order by id");
$re->bindParam(':pid', $pid);
$re->execute();
foreach($re as $re) {
$name=$re["name"];
$comment=$re["comment"];
?>
<div class="item" id="item-<?php echo $results['id']?>">
<p><img src="../upload/images/<?php echo $results['image']?>" height="500px" width="500px"</p>
<p><?php echo $results['caption']?></p>
<div class="large-2 columns small-3"><font color="#3366ff"><?php echo $name?></font></div>
<div class="large-10 columns"><p><?php echo $comment?></p></div>
<?php}
}?>
Thanks.
You were looping everything inside inner loop, try like this.
<?php
//This is for the images
$results = $connecDB->prepare("select image,caption,id from gallery order by id desc LIMIT $start, $limit ");
$results->execute();
$results = $results->fetchAll();
foreach($results as $results) {
$pid=$results["id"];
?>
<div class="item" id="item-<?php echo $results['id']?>">
<p><img src="../upload/images/<?php echo $results['image']?>" height="500px" width="500px" /></p>
<p><?php echo $results['caption']?></p>
<?php
//This is for the comments
$re= $connecDB->prepare("select * from comments where pid = :pid order by id");
$re->bindParam(':pid', $pid);
$re->execute();
foreach($re as $re) {
$name=$re["name"];
$comment=$re["comment"];
?>
<div class="large-2 columns small-3"><font color="#3366ff"><?php echo $name?></font></div>
<div class="large-10 columns"><p><?php echo $comment?></p></div>
<?php}?>
</div>
<?php}?>
Actually you are not getting the result in $re variable. You need to fetchAll data after executing the query. As well as you were looping everything in inner foreach.
foreach($results as $results) {
$pid=$results["id"];
//This is for the comments
$re= $connecDB->prepare("select * from comments where pid = :pid order by id");
$re->bindParam(':pid', $pid);
$re->execute();
$comments = $re->fetchAll();
<div class="item" id="item-<?php echo $results['id']?>">
<p><img src="../upload/images/<?php echo $results['image']?>" height="500px" width="500px"</p>
<p><?php echo $results['caption']?></p>
<div class="large-2 columns small-3"><font color="#3366ff"><?php echo $name?></font></div>
foreach($comments as $re) {
$name=$re["name"];
$comment=$re["comment"];
?>
<div class="large-10 columns"><p><?php echo $comment; ?></p></div>
<?php}
}?>
I am working on a online shopping website.
This is a div that will display the item and the image should be also used from database.
Help me!
This is the code by which I am trying to fetch the data from mysql database created on phpmyadmin and construct this div.
The problem is that I can't see any output
<?php
include "connect_database.php";
$sql="SELECT * FROM product";
$result = mysqli_query($connection, $sql);
$rows=mysqli_fetch_assoc($result);
while ($rows=mysqli_fetch_assoc($result)) {
echo "<div class='item'>
<img class='image' src='".$rows["image"]."'><br/>
<span>".$rows["product_name"]."</span><hr/>
<span>".$rows["model_no"]."</span><hr/>
<span>Rs ".$rows["amount"]."</span>
</div>
";
}
?>
You call mysqli_fetch_assoc first before the while loop. That call reads the first row, but the result is not used. After that, the while loop kicks in and processes and outputs the rest of the rows. If you have only one row, you won't see anything.
So you can try this, with that line removed.
Note that I renamed $rows to $row. This isn't necessary, but since the variable will contain data of only one row, I think this name is better. It will help you understand the code better if you need to change it later.
<?php
include "connect_database.php";
$sql="SELECT * FROM product";
$result = mysqli_query($connection, $sql);
while ($row=mysqli_fetch_assoc($result)) {
echo "<div class='item'>
<img class='image' src='".$row["image"]."'><br/>
<span>".$row["product_name"]."</span><hr/>
<span>".$row["model_no"]."</span><hr/>
<span>Rs ".$row["amount"]."</span>
</div>
";
}
?>
echo '<div class="item">
<img class="image" src="'.$rows['image'].'"><br/>
<span>"'.$rows["product_name"].'"</span><hr/> <span>"'.$rows['model_no'].'"</span><hr/>
<span>Rs "'.$rows['amount'].'"</span>
</div>
';
<?php
include "connect_database.php";
$sql="SELECT * FROM product";
$result = mysqli_query($connection, $sql);
while ($rows=mysqli_fetch_array($result,MYSQLI_BOTH)) {
echo "<div class='item'>
<img class='image' src="<?php echo $rows['image']; ?>"><br/>
<span><?php echo $rows['product_name']; ?></span><hr/>
<span><?php echo $rows['model_no']; ?></span><hr/>
<span>Rs<?php echo $rows['amount']; ?></span>
</div>
";
}
?>
I have a question about my code. When i try to fill my html with my fetched array i only get blank spaces instead of the actual results. Can anyone tell me why ?
<?php
require_once("php/loader.inc.php");
include("php/header.php");
include("php/userpanel.php");
$gebruiker = $_SESSION['user'];
?>
<div class="large-8 columns">
<div class="row">
<?php
$query = "
SELECT
items.naam,
items.prijs,
items.image_url
FROM inventory
JOIN items
ON items.id = inventory.item_id
WHERE
gebruiker_id = ?
";
$stmt = $db->prepare($query);
$stmt-> bind_param('i', $gebruiker->id);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc())
{
$user_items[] = $row;
echo '<div class="large-3 small-4 columns">
<img src="'. $user_items['image_url'] .'">
<div class="panel">
<h6>'. $user_items['naam'] .'</h6>
<h6 class="subheader">'. $user_items['prijs'] .'</h6>
</div>
</div>';
}
?>
By the way, when i try print_r($user_items); it gives me the expected result.
Thanks in advance!
I think you want to change this:
$user_items[] = $row;
to this:
$user_items = $row;
Otherwise your indexes are 1 dimension 'deeper'
As an example:
$row = array("image_url" => "xy");
after this line:
$user_items[] = $row;
You can't access it with:
echo $user_items["image_url"]; //Wrong
you would have to do this:
echo $user_items[0]["image_url"]; //works
You don't need this line $user_items[] = $row; you can use $row directly
while ($row = $result->fetch_assoc())
{
echo '<div class="large-3 small-4 columns">
<img src="'. $row['image_url'] .'">
<div class="panel">
<h6>'. $row['naam'] .'</h6>
<h6 class="subheader">'. $row['prijs'] .'</h6>
</div>
</div>';
}
The query am running against my database to get the 3 records order it by Random. The problem is that sometimes it shows all 3 records sometimes it only shows 2, 1 and other times its just blank. In the database I have around 28 records.
What I have tried
I have tried without LIMIT - Problem Same
I have echoed out $suggested_profile_id found all 3 records coming out.
This is the query that gets the records LIMIT it by 3
<?php
$sql = "SELECT * FROM members WHERE member_status='activated' ORDER BY RAND() DESC LIMIT 3";
$query = $db->SELECT($sql);
if($db->NUM_ROWS() > 0){
$rows = $db->FETCH_OBJECT();
?>
This is the code that runs and gets all 3 records in a loop.
<!-- Suggested Friends -->
<div class="col-md-0 media-body">
<?php
foreach($rows as $row){
$member_id = $row->member_id;
$sql = "SELECT * FROM profile WHERE profile_id='$member_id' LIMIT 1";
$query = $db->SELECT($sql);
$rows = $db->FETCH_OBJECT();
foreach($rows as $row){
$suggested_profile_id = $row->profile_id;
$suggested_profile_photo = $row->profile_photo;
$suggested_profile_username = $row->profile_username;
$suggested_profile_name = $row->profile_name;
if(
$suggested_profile_id != GET_SESSION_ID_VALUE(ENCRYPTION_KEY)&&
!is_in_ARRAY($make_string_to_ARRAY, $suggested_profile_id)
){
?>
<div class="row margin0">
<div class="col-md-4 pad0">
<a href="/<?php echo $suggested_profile_username; ?>" title="<?php echo $suggested_friends_profile_name; ?>" >
<?php
global $suggested_friends_profile_id;
$member_dir = dirname(dirname(dirname(__FILE__))) . "/members/" . $suggested_profile_id ."/smalll_" . $suggested_profile_photo;
if(file_exists($member_dir)){
?>
<img alt="<?php echo $suggested_profile_name; ?>" title="<?php echo $suggested_profile_name; ?>" src="/members/<?php echo $suggested_profile_id; ?>/smalll_<?php echo $suggested_profile_photo; ?>" width="50" height="50">
<?php
} else {
?>
<img alt="<?php echo $suggested_profile_name; ?>" title="<?php echo $suggested_profile_name; ?>" src="/assets/images/default.jpg" width="50" height="50">
<?php
}
?>
</a>
</div>
<div class="col-md-8 pad0">
<?php echo $suggested_profile_name; ?>
<span class="f12 gray">271 Mutual Friends</span>
Add as friend
</div>
</div>
<?php
}
}
}
?>
</div>
<!-- ** Suggested Friends -->
What am I missing? Is there any alternative way I can achieve this...thanks!
It looks to me like you're overwriting your $rows variable within the inner select.
foreach($rows as $row){ // <-- first $rows / $row
$member_id = $row->member_id;
$sql = "SELECT * FROM profile WHERE profile_id='$member_id' LIMIT 1";
$query = $db->SELECT($sql);
$rows = $db->FETCH_OBJECT(); <-- $rows overwritten
foreach($rows as $row){
Break your display from your application logic and you won't have such a hard time debugging this kind of thing. Besides, you have a lot of duplicated code and that makes things hard to manage as well as being hard to debug.
Further, you wouldn't have this problem if you ran one query: SELECT * FROM members JOIN profile ON members.member_id = profile.profile_id and not only does your code get simpler and your double-foreach loop problem disappear, but your database access will also be a lot more efficient.