PHP Separating Contents from While Loop? - php

Okay so my code below echoes out two URLs from the database but I want to have the ability to separate the two and update the row where the image is clicked. Example: Two images are output, users clicks image on the left, MySQL knows which row to update depending on which image they clicked. Depending on the image they click it will update the image's votes in MySQL. Ideally I want the "vote-ups" to show in place of the 112 and 156 accordingly from the database.
Let me rephrase to clarify: my code echoes out two images selected from the database. The code should tell you that if you read it. From there, I want to be able to separate the two images so I can do a query to update the specific row with that image and update the total votes that image has from votes to votes+1. I tried doing this but it would update both row's votes by 1.
How can I do this?
<?php
include_once('db.php');
$selectURLSQL = "SELECT * FROM `urls` ORDER BY RAND() LIMIT 3";
$selectURLQuery = mysql_query($selectURLSQL);
$URL_Row = mysql_fetch_array($selectURLQuery);
?>
<?php
while($URL_Row = mysql_fetch_array($selectURLQuery)) {
?>
<img class="img" name="img" src="<?php echo $URL_Row['url']; ?>" style="height:400px;width: 260px;">
<?php
}
?>
<br /><br />
<span style="font-size:18px;font-family:trebuchet ms;">156 Vote-Ups</span>
<small>Vs.</small>
<span style="font-size: 18px; font-family: trebuchet ms;">112 Vote-Ups</span><br /><br />

Say your table urls is structured like this:
id | url | rank
you have to increment the rank column for each click on the image corresponding to the url.
my idea is to create a link around the image, and this link will contain the id of the image so it can be passed in the URL like this ?id=xxx
if you want to show the votes outside the loop, you can save them temporarily in an array
Your Code:
<?php
include_once('db.php');
$selectURLSQL = "select * from urls ORDER BY RAND() LIMIT 2";
$selectURLQuery = mysql_query($selectURLSQL);
if (isset($_GET['id'])){
$id = $_GET['id'];
$sql = "UPDATE urls set rank = rank+1 WHERE id = $id";
mysql_query($sql);
}
$i = 0;
$votes = array();
while ($URL_Row = mysql_fetch_array($selectURLQuery)):
$votes[$i++] = $URL_Row['rank'];
?>
<a href="?id=<?php echo $URL_Row['id']; ?>">
<img class="img" name="img" src="<?php echo $URL_Row['url']; ?>" style="height:400px;width: 260px;">
</a>
<?php endwhile; ?>
<br /><br />
<span style="font-size:18px;font-family:trebuchet ms;"><?php echo $votes[0]; ?> Vote-Ups</span>
<small>Vs.</small>
<span style="font-size: 18px; font-family: trebuchet ms;"><?php echo $votes[1]; ?> Vote-Ups</span><br /><br />

Related

Displaying image name under image using PHP

I have developed a page where i display image from the database along with its image name.
I managed to display the names and imagenames from the database successfully, but the images and imagenames are aligned like:
IMG1 IMG2 IMG3 IMGname1 IMGname2 IMGname3
The code i tried to display the images and its name from the database:
<div class='badgeicon'>
<?php
$sql = "SELECT * FROM badges WHERE badge_id='$id_badge'";
$result = mysqli_query($db,$sql) or die(mysqli_error($db));
while($row = mysqli_fetch_array($result)){
$image_src2 = $row['badge_image'];
$imagedes = $row['description'];
?>
<img src= "<?php echo $image_src2; ?>" width="130px" height="140px">
<?php echo $imagedes; ?>
I expect the output to be :
IMG1 IMG2 IMG3
IMGname1 IMGname2 IMGname3
i.e., with the imagenames directly under the respective images.
I think your best bet is to format with css.
<div class="image">
<img src= "<?php echo $image_src2; ?>" width="130px" height="140px">
<span class="description"><?php echo $imagedes; ?></span>
</div>
And here for the style
.image{
display: inline-block;
}
.description{
bottom: 0;
}
You can do a lot more depending on exactly what you are trying to achieve.
I think this should be what you are looking for, keeping the img and name in a loop:
<div class='badgeicon'>
<?php
$sql = "SELECT * FROM badges WHERE badge_id='$id_badge'";
$result = mysqli_query($db,$sql) or die(mysqli_error($db));
$fetchresult = mysqli_fetch_array($result);
foreach($fetchresult as $row){
echo '<img src="'.$row['badge_image'].'" width="130px" height="140px">';
echo $row['description'];
}
?>

Inserting PHP database into HTML table

Hey I've recently been making a website and want to display the data from my database in a grid format opposed to it just listing down the page.
Here is my code right now:
<p>
<a href="pokemondetails.php?dex=<?php echo $row['dex'];?>">
<?php echo $row['name']; ?>
<br>
<img src="assets/<?php echo $row['dex']?>.png">
</a>
</p>
I was wondering how I would go about creating a for loop to allow the data from this database in conjunction with the image to span across the page with 7 columns and however many rows down until it reaches the end of the database.
Thanks!
<?php
$query = "Select * from tablename";
$bind = $conn->query($query);
if ($bind->num_rows > 0){
while ($row = $bind->fetch_assoc()){
?>
<p>
<a href="pokemondetails.php?dex=<?php echo $row['dex'];?>">
<?php echo $row['name']; ?>
<br>
<img src="assets/<?php echo $row['dex']?>.png">
</a>
</p>
<?php
}
}
?>
Try this, I just add while loop until End Of file (EOF table)

Echo pictures based on ID

I have a products page with full of product pictures with unique id from database for every product. When i click the product, it directs me to another page with unique id for each product. I need the page to show pictures based on the unique id of the product.
The page to click the product CODE:
<a href="productdetails.php?id=<?php echo $row['id']; ?>">
<img src="images/<?php echo $row["image"]; ?>" class="img-responsive" />
</a>
I need the clicked product page to show pictures based on the unique id.
CODE:
<?php
$getimagefrom = "SELECT image FROM product_details_image WHERE imagecode = 1 ORDER BY id";
$result = mysqli_query($connect, $getimagefrom);
while($row = mysqli_fetch_array($result))
{
?>
<img class="imgStyle" src="images/<?php echo $row["image"]; ?>" />
<?php
}
?>
The ORDER BY id is unique code that should be called when the product with the unique code is clicked.However, whenever i click the product, it goes to the page with unique id but the product shown is the same id always.
May ask question if not clear of it.

foreach and or if array statement

I have a profile that shows profiles in a list. as shown in the image below.
users table
id | email | full_name | job_title | bio | profile_photo
images table
image_id | id | artist_img
CODE
<?php
$db = dbconnect();
$stmt = $db->prepare('SELECT
users.email,
users.full_name,
users.job_title,
users.bio,
users.profile_photo,
images.id,
images.artist_img
FROM users
INNER JOIN images ON users.id=images.id GROUP BY images.id');
$stmt->execute();
$result = $stmt->get_result();
while (($row = mysqli_fetch_assoc($result)) != false) {
$id = $row['id'];
$full_name = $row['full_name'];
$email = $row['email'];
$job_title = $row['job_title'];
$bio = $row['bio'];
$ProfilePhoto = $row['profile_photo'];
$artist_img = $row['artist_img'];
if (isset($ProfilePhoto) && ! empty($ProfilePhoto)) {
$image = "$ProfilePhoto";
} else {
$image = "avatar.jpg";
}
echo "<div class='container team-wrap'>
<div class='row'>
<div class='col-md-6'>
<img class='img-responsive' src='artist/$image'>
</div>
<div class=\"col-md-6\">
<strong>$full_name<br>$job_title</strong>
<br>
<p>$bio</p>
<a href='mailto:$email' class='btn btn-info'>Contact Me</a>
</div>
</div>
</div>
<div class=\"container space team-wrap\">
<div class=\"row\">
<div class=\"col-lg-12\">
<div id=\"gallery-slider\" class=\"slider responsive\">
<div>";
echo"
<img src=\"gallery/$artist_img\" alt=\"\"></a>";
echo "</div>
</div>
<hr>
</div>
</div>
</div>";
}
?>
Problem area
echo"<img src=\"gallery/$artist_img\" alt=\"\"></a>";
The issue I am having is that it repeats the profile for each image if the user has 5 images it will add 5 profiles 1 for each img.
and does not show the other users profile at all. show how it shows up is look at the image for example its got 4 images under profile 1 and it shows there profile pic.. well it repats all that info for each image I want the pics that have the same id as the user to show up as a slider like below..
and it also refuses to show the other profiles of other users.
yes because it is not seeing the variable because you echo it as text
echo"<img src=\"gallery/$artist_img\" alt=\"\"></a>";
should be
$r=0;
foreach ($images as $image.id){
[$artist_img=$images[$r];
echo "<img src=\"gallery/".$artist_img."\" alt=\"\"></a>";
$r++;
}
//
i don't like to echo to much html because very easy to make a mistake that way i prefer it is to stay in html and just echo my variable like the but that is just me
<html>
<body>
<a><img src="gallery/<? php echo $artist_img; ?>" alt=""></a>
</body>
</html>
do you see here you fetch the variable for the picture as single row
$artist_img = $row['artist_img'];
you need to make it a array because it is a array you must remember using inner generates a number of pieces of data, maybe for you it will be better to run 2 query second query loads the images.inner join is useful for some people but complex and dont really give any advantage because still searches the whole tables twice to get the results
something like this might work for for you
$artist_img2=array();
//because there are more then one piece of data in it
$artist_img2 = $row['images'];
//then you need to do another loop to put the data in variables or echo them out
// in the loop
//note the row refers to id and not image.id because in the inner array the key will be id
$artist_img3=row2['id'];
echo "<img src=\"gallery/".$artist_img3."\" alt=\"\"></a>";
//end loop

Two query at the same time

Hello first of all what i am doing in , i am coding a website for advertise .
Now what do i need is a help to display a lots of data from two tables of database .
What i have done so far u can check at My project you have to login use (Username : test , password : 123456a) to login , so there is everything is okay except an image image are the same on every ads and i do not find the way to make it right .
So i have a "posts" table with an information about ads and an "images" table with a path of an image this is how its looks like :
and this is my code :
<?php
$userid = $_SESSION["userid"];
$sql = "SELECT * FROM posts WHERE userid='$userid' ";
$res = mysqli_query($connect,$sql);
while ($row = mysqli_fetch_assoc($res)) {
?>
<div id="ads">
<div id="titlepic">
<?php echo $row["title"]; ?><br>
<img src="<?php echo $Photo[0]; ?>" height="100px;">
</div>
<div id="managead">
Edit<br style="margin-bottom: 5px;">
Delete<br style="margin-bottom: 5px;">
Renew
</div>
<div id="dates">
<b>Date Added:</b> <?php echo date('m/d/Y', $row["dateadded"]); ?><br>
<b>Renew Date:</b> <?php if($row["renewdate"] > 0){ echo date('m/d/Y', $row["renewdate"]); } ?><br>
<b>Location:</b> <?php echo $row["location"]; ?><br>
<b>Price:</b> <?php echo $row["price"]; ?><br>
</div>
</div>
<hr width="100%">
<?php
so the question is how to extract and images from other table at the same time or how tu run two query at the same time and get an information from them
your SQL statement needs a JOIN in order to include data from two tables in one query.
$sql = "
SELECT *
FROM posts p
JOIN images i
ON p.id = i.postid
WHERE p.userid='$userid'
";
this result set will be populated with all columns from both tables. now you can access path1 via:
<?php echo $row["path1"]; ?>
while this will work for all of your images, such as $row["path2"], $row["path3"], etc, keep in mind this is a bad design for a many-to-many relationship, so it should be normalized to include a linking table which would hold all of your images.

Categories