i'm newbie on this.
I explain shortly what i need.
Obtained the category ID via $_GET i want to print how images i have for each product (for that category).
For example, i have 2 product and 2 images for each product.
Problem found: with this code i have printed only the images for the first product and not for the second. Why? Thanks
$id=17;
$prod = "SELECT pd_id FROM product WHERE cat_id = ?";
$stmt = $con->prepare($prod);
$stmt->bindParam(1, $id);
$stmt->execute();
$num = $stmt->rowCount();
if($num>0)
{
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
$immagini = "SELECT pd_image FROM pd_images WHERE pd_id = ?";
$stmt = $con->prepare($immagini);
$stmt->bindParam(1, $pd_id);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
?>
<img src="<?php echo $home_url; ?>images/product/<?php echo $pd_image; ?>" class="img-fluid border-radius-0" alt="">
<?php
}
}
}else { ?>
<div class="col-lg-12 text-center">
<h6>There's no products for this category</h6>
<i class="rt-icon2-user"></i>
<div class="divider-10 d-none d-xl-block"></div>
</div>
<?php } ?>
Related
I'm trying to get it where it will get the friends id from the friends table, and then get the friends information from the users table. I've tried using a foreach but had no luck.
Here's what I have right now where it's only echoing one friend and not the three that I have in the table. Any ideas on how I can fix this issue? Maybe I wasn't using the for each properly? Thank You In Advance!
<?php
//Gets users information from users.
$stmt = $DB_con->prepare('SELECT friendsid FROM friends WHERE userid='.$_SESSION['user']['id']);
$stmt->execute();
if($stmt->rowCount() > 0) {
$row=$stmt->fetch(PDO::FETCH_ASSOC);
extract($row);
} else {
$friendsid = $row['friendsid'];
}
$stmt = $DB_con->prepare('SELECT username,userprofile,status FROM users WHERE id='.$friendsid);
$stmt->execute();
if($stmt->rowCount() > 0) {
while($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
extract($row);
?>
<div class="row">
<div class="col-sm-3">
<div class="well">
<h4><strong><?php echo $row['username'];?></strong></h4>
<img src="images/profile/<?php echo $userprofile;?>" class="img-circle" height="70" width="70" alt="Avatar">
</div>
</div>
<div class="col-sm-9">
<div class="well">
<h3 class="text-left"><?php echo $row['status'];?></h3>
</div>
</div>
</div>
<?php
}
} else {
?>
<?php
}
?>
Prepared statements use parameters and you should use a JOIN.
$stmt = $DB_con->prepare('SELECT u.* FROM users AS u JOIN friends AS f ON u.id = f.friendsid WHERE f.userid = :user_id');
$stmt->bindParam(':user_id', $_SESSION['user']['id']);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
// Do whatever you want with $row['id'], $row['username'], $row['userprofile']
}
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 can't seem to return any values on my $_GET array.
It works fine when e.g.
$sql = "SELECT * FROM review WHERE brand='brandx'" but when I change it to brand='$id' in line 5, nothing gets passed.
The fetch array in my index.php works perfectly fine however when it gets href to brand.php (as shown below), I lose my marbles.
<?php
if(isset($_GET["id"])){
include "php_includes/db_conx.php";
$id = preg_replace('#[^0-9]#i', '', $_GET["id"]);
$sql = "SELECT * FROM review WHERE brand='$id'";
$query = mysqli_query($db_conx, $sql);
$productList = "";
// Now make sure that brand exists in the table
$productCount = mysqli_num_rows($query);// count the output amount
if($productCount > 0){
//get the products off the selected brand
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$username = $row['username'];
$productname = $row['productname'];
$comment = $row['comment'];
$rating = $row['rating'];
$date = $row['date'];
$productList .=
'
<div class="wrapper">
<div class="brand-and-name">
<div class="brand">
<a href="brand.php?id='.$id.'">
<span>'.$id.'</span>
</a>
</div>
<div class="prod-name">
'.$productname.'
</div>
</div>
<div class="prod-container" id="pd1">
<div class="prod-img"><img src="https://giovanniphotography.files.wordpress.com/2011/09/creativemevid19.jpg" /></div>
<div class="comment">
<b>My Score: '.$rating.'/10</b>
<br /><br />
<p>'.$comment.'</p>
</div>
<div class="profile">
<div class="profile-thumb" id="pt1"></div>
<div class="name" id="nm1">
'.$username.'<br />'.$date.'
</div>
</div>
<div class="social-share-1">
<div class="like-btn"></div>
<div class="comment-btn"></div>
<div class="wishlist-btn">+ wishlist</div>
</div>
</div><!--end .prod-container#pd1-->
</div><!--wrPer-->
';
}
}else{
echo "Product doesnt exist";
exit ();
}
}else{
echo "You got to pick a brand man!";
exit ();
}
?>
Does this code work, after your preg_replace? If not you may not have magic quotes enabled in php.ini. I notice in the rest of your output you are concatenating strings and variables.
print "ID: $id";
use a prepared statement and dont hassle with sanitizing of user input:
if($stmt = $db_conx->prepare("SELECT username, productname, comment, rating, date FROM review WHERE brand=?)")
{
$stmt->bind_value('s', $_GET["id"]);
$result = $stmt-execute();
$stmt->bind_result($username, $productname, $comment, $rating, $date ); //bind result to vars
//now you can loop through your result:
while($stmt->fetch()) {
//use $username, $productname, $comment, $rating, $date etc to work with your values
}
}
I'd like to put images next to each other when they are selected from a while loop in PHP.
So, currently it looks like this http://prntscr.com/7tb42j
And I'd like it to put the images next to each other.
My foreach loop looks like this:
<div id='profile-album'>
<?php
$get_fotos = "SELECT * FROM fotos_profile WHERE username=:username LIMIT 4";
$stmt = $db->prepare($get_fotos);
$stmt->bindParam(':username', $username);
$stmt->execute();
foreach($stmt as $row)
{
$pic = $row['pic'];
$title_foto = $row['title'];
?>
<div id='album-foto-1'><img src="userdata/profile_fotos/<?php echo $pic; ?>" height='100px' width='206px' style='padding-right: 6px'/></div>
<?php } ?>
You'll just need to add display:inline-block to each div that contains a picture.
<div id='profile-album'>
<?php
$get_fotos = "SELECT * FROM fotos_profile WHERE username=:username LIMIT 4";
$stmt = $db->prepare($get_fotos);
$stmt->bindParam(':username', $username);
$stmt->execute();
foreach($stmt as $row)
{
$pic = $row['pic'];
$title_foto = $row['title'];
?>
<div id='album-foto-1' style="display:inline-block"><img src="userdata/profile_fotos/<?php echo $pic; ?>" height='100px' width='206px' style='padding-right: 6px'/></div>
<?php } ?>
Make sure on "album-foto" you have the css set for (see below why I use album-foto not album-foto-1):
.album-foto {
display:inline; // or inline-block depending how you want to be displayed
}
Also if you're displaying multiple images you should use a class not an ID for the images as duplication of the same ID is not good:
foreach($stmt as $row)
{
$id = $row["id"]; // or whatever your ID field is
$pic = $row['pic'];
$title_foto = $row['title'];
?>
<div class='album-foto' id='album-foto-<? echo $id; ?>'><img src="userdata/profile_fotos/<?php echo $pic; ?>" height='100px' width='206px' style='padding-right: 6px'/></div>
<?php } ?>
$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