I am creating a row of "product related table of x5" and using MYSQLcode RAND to generate RANDOM products and a exception clause using id!='$id' - where id is existing product that is being viewed currently.
However the problem is that all the product images generated were all of the same similar product image.
Can anyone shed some light on this?
Pic for better reference:
include "MyOnlineStore/storescripts/connect_to_mysql.php";
$sql2 = mysql_query("SELECT * FROM bag WHERE id!='$id'ORDER BY RAND()LIMIT 5");
$productCount2 = mysql_num_rows($sql2); // count the output amount
if ($productCount2 > 0) {
?>
<table border="1">
<tr>
<?php
while ($row2 = mysql_fetch_array($sql2)) {
$idrelated = $row2["id"];
$imagerelated = $row2["image"];
$titlerelated = $row2["title"];
}
} else {
echo "That item does not exist.";
exit();
}
mysql_close();
HTML
<table border="1">
<tr>
<td>
<a href="http://example.net/product.php?id=<?php echo $idrelated; ?>">
<img src="admin/product/uploaded_files/<?php echo $imagerelated; ?>" width="100" height="100" alt="<?php echo $titlerelated; ?>" />
</a>
</td>
<td>
<a href="http://example.net/product.php?id=<?php echo $idrelated; ?>">
<img src="admin/product/uploaded_files/<?php echo $imagerelated; ?>" width="100" height="100" alt="<?php echo $titlerelated; ?>" />
</a>
</td>
<td>
<a href="http://example.net/product.php?id=<?php echo $idrelated; ?>">
<img src="admin/product/uploaded_files/<?php echo $imagerelated; ?>" width="100" height="100" alt="<?php echo $titlerelated; ?>" />
</a>
</td>
<td>
<a href="http://example.net/product.php?id=<?php echo $idrelated; ?>">
<img src="admin/product/uploaded_files/<?php echo $imagerelated; ?>" width="100" height="100" alt="<?php echo $titlerelated; ?>" />
</a>
</td>
<td>
<a href="http://example.net/product.php?id=<?php echo $idrelated; ?>">
<img src="admin/product/uploaded_files/<?php echo $imagerelated; ?>" width="100" height="100" alt="<?php echo $titlerelated; ?>" />
</a>
</td>
</tr>
</table>
Your code should be something like this
include "MyOnlineStore/storescripts/connect_to_mysql.php";
$sql2 = mysql_query("SELECT * FROM bag WHERE id!='$id'ORDER BY RAND()LIMIT 5");
$productCount2 = mysql_num_rows($sql2); // count the output amount
if ($productCount2 > 0) {
?>
<table border="1">
<tr>
<?php
while ($row2 = mysql_fetch_array($sql2)) {
$idrelated = $row2["id"];
$imagerelated = $row2["image"];
$titlerelated = $row2["title"];
?>
<td>
<a href="http://example.net/product.php?id=<?php echo $idrelated; ?>">
<img src="admin/product/uploaded_files/<?php echo $imagerelated; ?>" width="100" height="100" alt="<?php echo $titlerelated; ?>" />
</a>
</td>
<?php
}
?>
</tr>
</table>
As far as I can see from your code, the following variables:
$idrelated, $imagerelated, $titlerelated
remains unchanged after leaving the loop. So in each line you refer to the same variable and value.
If you want to keep use some array to keep all five values:
<?php
$index = 0;
while (...) {
$relatedA[$index] = ...;
$relatedB[$index] = ...;
$relatedC[$index] = ...;
$index++;
}
?>
...
<?php echo $relatedA[0]; ?>
<?php echo $relatedA[1]; ?>
...
<?php echo $relatedA[4]; ?>
The last you can put in the loop again to avoid redundant code.
UPDATE
Try to resist the temptation of putting echoes into the first loop where you fetch values - as it could result in hard to maintenance spaghetti-style code (unfortunately quite commonly seen in many php sources).
Related
I have a little problem getting an image...
That is what I have:
<td data-label="Fotografija">
<?php
if($row["fotografija"] == "") {
echo '<img class="myImg" src="images/nislike.png" alt="Fotografija ni na voljo." style="max-width: 20%; min-width:100px;">';
} else {
echo '<img class="myImg" src="templates/slike/fotografije/?><?php
echo $row["fotografija"]; ?>" alt="<?php echo $row["ime"];?> <?php
echo $row["priimek"]; ?>" style="max-width: 20%; min-width:100px;">';
}?>
</td>
The result is not working like it should.
I am getting the default picture (images/nislike.png) like expected but if there is an entry in mysql, it's not working.
First you need to check that is $row have array or its empty.
var_dump($row);
Check, it should have some value or empty for fotografija
<td data-label="Fotografija">
<?php if($row["fotografija"] == "") { ?>
<img class="myImg" src="images/nislike.png" alt="Fotografija ni na voljo." style="max-width: 20%; min-width:100px;">
<?php } else { ?>
<img class="myImg" src="templates/slike/fotografije/<?php echo $row["fotografija"]?>" alt="<?php echo $row["ime"].$row["priimek"]; ?>" style="max-width: 20%; min-width:100px;">
<?php } ?>
</td>
Remove ?> in this part:
templates/slike/fotografije/<?php echo $row["fotografija"];
I have my current code working perfectly but I needed help on when I want the code to display as many queries exist in the table called wine... for example if there are more than wine type in category as spain then it should display all but it only displays one.
Here is my PHP:
<?php
include"db_connection.php";
$sql = mysql_query("SELECT * FROM WINE WHERE country='Chile'");
while($row = mysql_fetch_array($sql)){
$description = $row["description"];
$wine_type = $row["wine_type"];
$country = $row["country"];
$bottle_price = $row["bottle_price"];
$indicator = $row["indicator"];
$colour = $row["colour"];
$case_price = $row["case_price"];
$case_size = $row["case_size"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
}
?>
Here is my HTML:
<?php include('header.php'); ?>
<div id="content">
<table width="100%" border="0" cellspacing="0" cellpadding="15">
<tr>
<td width="19%" valign="top"><img src="inventory_images/<?php echo $id; ?>.jpg" width="142" height="188" alt="<?php echo $wine_type; ?>" /><br />
View Full Size Image</td>
<td width="81%" valign="top"><h3><?php echo $wine_type; ?></h3>
<p><?php echo "$".$bottle_price; ?><br /><br />
<?php echo "$country $indicator"; ?> <br /><br />
<?php echo $description; ?> <br />
</p>
<form id="form1" name="form1" method="post" action="cart.php">
<input type="hidden" name="pid" id="pid" value="<?php echo $id; ?>" />
<input type="submit" name="button" id="button" value="Add to Shopping Cart" />
</form>
</td>
</tr>
</table>
</div>
<?php include('footer.php'); ?>
The problem is currently that the while() loop will keep overriding the variables. This can be solved in a couple of ways, one would be that you save the entire fetched column in an array and use that array later for iteration.
$allRows = array();
while($row = mysql_fetch_array($sql)) {
$allRows[] = $row;
}
Now, as mentioned above, iterate over $allRows in your template
<?php include('header.php'); ?>
<div id="content">
<table width="100%" border="0" cellspacing="0" cellpadding="15">
<?php
foreach ($allRows as $row) {
?>
<tr>
<td width="19%" valign="top"><img src="inventory_images/<?php echo $row['id']; ?>.jpg" width="142" height="188" alt="<?php echo $row['wine_type']; ?>" /><br />
etc.
</tr>
<?php
}
?>
</table>
</div>
<?php include('footer.php'); ?>
Here I addressed the variables as $row['...'] - if you don't want to change that part of the code, simply do the assigning at the beginning of the loop.
etc.
<?php
foreach ($allRows as $row) {
$description = $row["description"];
$wine_type = $row["wine_type"];
//etc.
?>
A much cleaner solution (not mixing that much HTML and PHP which usually creates lots of confusion) would be using a template engine. Also don't use mysql_* functions in new code - they are deprecated. See this answer for more information.
I have a database table and that table has 6 rows. What I want is to display that 6 rows in a html page using a 3 column and 2 row table.
I know how to work with php arrays and while loops. My problem is how to limit the array to put 3 items in the first row and put the other 3 in the next row.
this is what i have tried but didn't work
<div id="maincontent">
<!-- class one -->
<?php
$getSection = getSection();
$i=0;
while($allSection = mysql_fetch_array($getSection)){
?>
<div class="subconent">
<table width="937" border="0">
<tr>
<td>
<div class="sub_image">
<img src="admin/uploads/fron_sect/<?php echo $allSection['image']; ?>" width="134" height="120" border="0" alt="HNA" class="PopBoxImageLink" onmouseover="PopEx(this,-50,-25,205,186,20,null);" onclick="window.location='http://localhost/hants/section.php?id=<?php echo urlencode($allSection['id']); ?>'" />
</div>
<div class="cont_txt">
<h3><?php echo $allSection['name_full']; ?></h3>
<p><?php echo substr($allSection['description'],0,140) . ""; ?></p>
<br />
<img src="images/read_more.jpg" alt="Read More" width="89" height="25" border="0" />
</div>
</td>
</tr>
</table>
</div>
<?php
if($i==4) { ?>
<table width="937" border="0">
<tr>
<td> </td>
<td> </td>
<td> </td></tr>
<tr><div class="sub_image">
<img src="admin/uploads/fron_sect/<?php echo $allSection['image']; ?>" width="134" height="120" border="0" alt="HNA" class="PopBoxImageLink" onmouseover="PopEx(this,-50,-25,205,186,20,null);" onclick="window.location='http://localhost/hants/section.php?id=<?php echo urlencode($allSection['id']); ?>'" />
</div>
<div class="cont_txt">
<h3><?php echo $allSection['name_full']; ?></h3>
<p><?php echo substr($allSection['description'],0,140) . ""; ?></p>
<br />
<img src="images/read_more.jpg" alt="Read More" width="89" height="25" border="0" />
</div><td>
<?php }
} ?>
</div>
Use modulo operator (%):
http://www.devchunks.com/web-development/using-the-php-modulus-operator/
something like this:
<table>
<?php
$i = 0;
while ( $row = mysql_fetch_array($result) ){
if ($i % 3 == 0){
echo '<tr>';
}
echo '<td>'.$row['column_name'].'</td>';
if ($i % 3 == 2){
echo '</tr>';
}
$i++;
}
//here is a check in case you don't have multiple of 3 rows
if ($i % 3 != 0){
echo '</tr>';
}
?>
</table>
At its base, you'll need something like this:
<table>
<tr>
<?
$count = 0;
foreach ($row) {
echo "<td>" . $row["value"] ."</td>";
$count++;
if (($count % 3) == 0) && ($count > 0) {
echo ("</tr><tr>");
}
}
?>
</tr>
</table>
Start printing out the header of your table, and then begin iterating through the dataset. Keep track of how many you've printed out, and if this is the third one, print the HTML to finish this row and start the next one. (I've used %, so it'll wrap on every third entry, not just the first one)
Well, you could correctly fetch those informations in your sql-query ( just one example that could fit http://en.wikibooks.org/wiki/MySQL/Pivot_table ).
Or simply fetch everything into PHP arrays.
Oldschool: mysql_query() and while( $row = mysql_fetch_array() )
Newchool: PDO ( http://de.php.net/manual/en/book.pdo.php )
Awesome! Thanks a lot. It works for me, Zend. You can try something like this.
<table width="1024px" border="0" cellspacing="2" cellpadding="2">
<?php
$i = 0;
foreach ($this->rows as $row )
{
$img = IMAGE_PATH . '/' . 'gallery/' . $row->gly_thumbnail;
if ($i % 3 == 0)
{
echo '<tr>';
}
?>
<td align="center">
<img src="<?php echo $img; ?>" width="300" height="215"><br/>
<?php echo $row->gly_title; ?>
</td>
<?php
if ($i % 3 == 2)
{
echo '</tr>';
}
$i++;
}
//here is a check in case you don't have multiple of 3 rows
if ($i % 3 != 0)
{
echo '</tr>';
}
?>
</table>
<?php if ($i++%$_columnCount==0): ?>
<tr>
<?php endif ?>
<td> <img src="<?php echo site_url('uploads/shelter_images/'.$row->shelter_id."/".$img->imagefile) ?>" alt="" width="300" ></td>
<?php if ($i%$_columnCount==0 || $i==$totalImg): ?>
</tr>
<?php endif; ?>
<?php } ?>
I'm working on a php gallery. I'm displaying image from mysql db using php, but my images are displaying in one by one. which means the first image in first row and second image in second row. but I want to display my image as 3 or 4 per row. what coding changes can I make. my php code as shown below.
<?php
include_once("config.php");
$result=mysql_query("SELECT * FROM images");
while($res=mysql_fetch_array($result)){ ?>
<table width='200'>
<tr>
<td><?php echo"<a href='indimage.php?imageid=$res[imageid]'>"?><?php echo $res['imagename']?><?php echo"</a>"?></td>
</tr>
<tr>
<td>
<div id="news-image">
<?php echo"<a href='indimage.php?imageid=$res[imageid]'>"?>
<?php echo'<img src='.$res['image'].' width="250" height="100">'?><?php echo"</a>"?>
</div>
</td>
</tr>
</table>
<?php } ?>
array_chunk() is a function to split an array into a collection of X items for you to loop through without having to keep counters (you can then use array_pad() on the last item in the list if you need padding)
if($array = array_chunk(mysql_fetch_assoc($result),4))
{
foreach($array as $row)
{
echo '<div class="row">';
foreach($row as $col)
{
echo '<div class="item">' . $col['image'] . '</div>';
}
echo '</div>';
}
}
You're outputting a table per image. At minimum, your code should be more like this:
<table>
<tr>
<?php while($res etc...) { ?>
<td>
<img src="<?php echo ......?>" />
</td>
<?php } ?>
</tr>
</table>
Now you'll get all the images in a single row of a single table. Making it have multiple rows is left as an exercise to the OP.
follow this example
<table>
<tr>
<?php
$i = 1;
do{
echo "<td>" . $i . "</td>";
//Num of Columns
if( $i%3 == 0 ){
echo "</tr><tr>";
}
$i++;
}while($i<=10);
?>
</tr>
<table>
will return the result something like you want..
<?php
include_once("config.php");
$result=mysql_query("SELECT * FROM images");
?>
<table>
<tr>
<?php
$cnt = 0;
while($res=mysql_fetch_array($result))
{
if($cnt == 3){
echo "</tr><tr>";
}
?>
<td>
<table width='200'>
<tr>
<td><?php echo"<a href='indimage.php?imageid=$res[imageid]'>"?><?php echo $res['imagename']?><?php echo"</a>"?></td>
</tr>
<tr>
<td>
<div id="news-image">
<?php echo"<a href='indimage.php?imageid=$res[imageid]'>"?>
<?php echo'<img src='.$res['image'].' width="250" height="100">'?><?php echo"</a>"?>
</div>
</td>
</tr>
</table>
</td>
<?php
$cnt++;
}
?>
</tr>
</table>
Use the following code.
<?php
include_once("config.php");
$result=mysql_query("SELECT * FROM images");
?>
<table>
<tr>
<?
$varcount=0;
while($res=mysql_fetch_array($result))
{
$varcount++;
if($varcount == 4) // Count of images per row. 3 or 4
{
$varcount=0;
?>
</tr><tr>
<?
}
?>
<td>
<table width='200'>
<tr>
<td><?php echo"<a href='indimage.php?imageid=$res[imageid]'>"?><?php echo $res['imagename']?><?php echo"</a>"?></td>
</tr>
<tr>
<td>
<div id="news-image">
<?php echo"<a href='indimage.php?imageid=$res[imageid]'>"?>
<?php echo'<img src='.$res['image'].' width="250" height="100">'?><?php echo"</a>"?>
</div>
</td>
</tr>
</table>
</td>
<?php
}
?>
</tr>
</table>
You could instead output the images as a list of divs, or just divs, and then use CSS to show the images in two columns. Your layout should not be that hardwired.
<style>
div.gallery {
width: 650px;
}
div.gallery ul li {
list-style: none;
float: left;
}
div.image {
height: 500px;
width: 300px;
}
</style>
<div class="gallery">
<ul>
<li>
<div class="image">
<span class="image_title">Some title</span><br/>
<img src="foo.png"/>
</div>
</li>
<li>
<div class="image">
<span class="image_title">Another title</span><br/>
<img src="bar.png"/>
</div>
</li>
<li>
<div class="image">
<span class="image_title">Another title</span><br/>
<img src="foo.png"/>
</div>
</li>
<li>
<div class="image">
<span class="image_title">Another title</span><br/>
<img src="bar.png"/>
</div>
</li>
<li>
<div class="image">
<span class="image_title">Another title</span><br/>
<img src="foo.png"/>
</div>
</li>
</ul>
</div>
Result:
Your code should look something like:
<div class="gallery">
<ul>
<?php
include_once("config.php");
$result = mysql_query("SELECT * FROM images");
while($res = mysql_fetch_array($result)) {
?>
<li>
<div class="image">
<a class="image_title" href="indimage.php?imageid=<?php echo $res['imageid']?>"><?php echo $res['imagename']?></a><br/>
<img src="<?php echo $res['image']?>" />
</div>
</li>
<?php
}
?>
</ul>
</div>
Here is the Solution i think you want that:
there are three pages.
1. index.php (which has the form for uploading the image)
2. upload.php (which save the image in directory and its path in database)
3. showimage.php (Finally, which will show the image)
here is the code
(index.php)
<form method="post" action="upload.php" enctype="multipart/form-data">
<label>Choose File to Upload:</label><br />
<input type="hidden" name="id" />
<input type="file" name="uploadimage" /><br />
<input type="submit" value="upload" />
</form>
(upload.php)
<?php
$target_Folder = "upload/"; // directory where images will be saved
$target_Path = $target_Folder.basename( $_FILES['uploadimage']['name'] );
$savepath = $target_Path.basename( $_FILES['uploadimage']['name'] );
$file_name = $_FILES['uploadimage']['name'];
if(file_exists('upload/'.$file_name))
{
echo "That File Already Exisit";
}
else
{
// Database
$con=mysqli_connect("localhost","user_name","pasword","database"); // Change it if required
//Check Connection
if(mysqli_connect_errno())
{
echo "Failed to connect to database" . mysqli_connect_errno();
}
$sql = "INSERT INTO image (id,image, image_name)
VALUES ('$uid','$target_Folder$file_name','$file_name') ";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added successfully in the database";
echo '<br />';
mysqli_close($con);
// Move the file into UPLOAD folder
move_uploaded_file( $_FILES['uploadimage']['tmp_name'], $target_Path );
echo "File Uploaded <br />";
echo 'File Successfully Uploaded to: ' . $target_Path;
echo '<br />';
echo 'File Name: ' . $_FILES['uploadimage']['name'];
echo'<br />';
echo 'File Type: ' . $_FILES['uploadimage']['type'];
echo'<br />';
echo 'File Size: ' . $_FILES['uploadimage']['size'];
}
?>
Show Image
(showimage.php)
<?php
$con=mysqli_connect("localhost","user_name","password","database_name"); // Change it if required
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM image " );
while($row = mysqli_fetch_array($result))
{
echo '<img src="' . $row['image'] . '" width="200" />';
echo'<br /><br />';
}
mysqli_close($con);
?>
Features
It will check the names of file if that name file already exisit it will not uplad the file and alert the user.
Database Structure
id int(4) Auto Increment - image varchar(100) - image_name varchar(50)
I've been trying to pagginate my picture but very very unssucesfull i am hopping i can ghet some hints on how to do it. This is my code.
<?php
$result = mysql_query("SELECT * FROM images ORDER BY position");
while ($image = mysql_fetch_array($result)){
?>
<img src="images/uploads/thumbs/<?php echo $image['thumb'];?>" width="100%" height="100%" alt="" class="thumbnail" />
<input type='button' value='Preview' onclick="changedesc(<?php echo $image['image_id']; ?>); return false;" />
<?php }?>
This shoul'd do it..
<?php
$query = "SELECT * FROM images ORDER BY date";
$result = mysql_query($query);
while ($image = mysql_fetch_assoc($result)){
echo '
<img src="images/uploads/thumbs/'.$image['thumb'].'" width="100%" height="100%" alt="" class="thumbnail" />
';
}
?>