how to display an image in pagination? - php

How will i Display a picture with the same id here in my pagination.php?
i tried doing this
<td width="20%" valign="top"><a href="product.php?id=' . $id . '">
<img style="border:#666 2px solid;" src="inventory_images/' . $id . '.jpg" width="150" height="102" border="1" /></a>
</td>
but it wont work.. any possible way to display the image using pagination?
for ($i = $start; $i < $end; $i++)
{
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) { break; }
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . mysql_result($result, $i, 'product_name') . '</td>';
echo '<td>' . mysql_result($result, $i, 'price') . '</td>';
echo '<td>' . mysql_result($result, $i, 'details') . '</td>';
echo '<td>' . mysql_result($result, $i, 'category') . '</td>';
echo '<td>' . mysql_result($result, $i, 'subcategory') . '</td>';
echo '<td>Click To View</td>';
echo "</tr>";
}

Probably you have to put in your code something like that :
<td width="20%" valign="top">
<a href="product.php?id=<?php echo $id; ?>">
<img style="border:#666 2px solid;" src="inventory_images/<?php echo $id; ?>.jpg" width="150" height="102" border="1" />
</a>
</td>
Check the generated source and adjust the code above. Please keep in mind that in HTML you cannot put php variables directly. You have to output them with php echo statement like that :
<html>
SOME HTML
<div class="valueFromPhp"><?php echo $value; ?></div>
</html>
Also remebmer to escape output using htmlentities() to prevent XSS attacks.

echo '
<td>
<img width=100 src="../PHP/saved_images/'.mysql_result($result, $i, "bkimg").'">
</td>
';

Related

Replacing my "Add to Cart" link with "Out of Stock"

I'm trying to, for the sake of a class project, create a mock-up store. My tools are XAMPP 3.2.2 and phpMyAdmin. An actual, functioning "Add to cart" button will come later(Right now I'm just using a link as a placeholder), but for the time being, I'm trying to figure out how to replace the button/link with a message reading "Out of Stock" when the Stock is 0.
Here's the code I'm using to display the product page; it's almost certainly messy, and there's probably a dozen better ways to do it, but for now, it gets the job done:
<?php
$sql = "SELECT * FROM webstore.Products order by category";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<table border="2" width="100%">';
echo '<tr>';
echo ' <td width="20%">' . $row["Name"] . '</td>';
echo ' <td width="20%">' . $row["Descr"] . '</td>';
echo ' <td width="20%">' . $row["Price"] . '</td>';
echo ' <td width="20%">' . '<img src =' . $row["IconURL"] . '>' . '</td>';
echo ' <td width="20%">' . 'Add to cart' . '</td>';
echo ' </tr> ';
echo '</table>';
}
} else {
echo "0 results";
}
The output looks like this:
Results
I'm not looking for "better practices;" right now, I just need a way to replace the shopping cart link with "Out of Stock" when the product stock is 0.
add a check to the while loop some what like:
while($row = $result->fetch_assoc()) {
echo '<table border="2" width="100%">';
echo '<tr>';
echo ' <td width="20%">' . $row["Name"] . '</td>';
echo ' <td width="20%">' . $row["Descr"] . '</td>';
echo ' <td width="20%">' . $row["Price"] . '</td>';
echo ' <td width="20%">' . '<img src =' . $row["IconURL"] . '>' . '</td>';
if($row['stock'] >0){
echo ' <td width="20%">' . 'Add to cart' . '</td>';
}else{
echo '<td width="20%">Out of Stock</td>';
}
echo ' </tr> ';
echo '</table>';
}

How to convert this echo? using this approach

how to convert this?
<td><a style="font weight: 700; font-size:1.2em;" href="preview.php? app_id=<?php echo $row['app_id']; ?>name=<?php echo $row['name']; ?>">PRINT</a> <!-- Delete --></td>
into this?
echo '<td>' . $row['curdate'] . '</td>'
here's the full code
<tr class="success">
<td><?php echo $row['app_id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['addr']; ?></td>
<td><?php echo $row['contact']; ?></td>
<td><?php echo $row['curdate']; ?></td>
<td><a style="font weight: 700; font-size:1.2em;" href="preview.php? app_id=<?php echo $row['app_id']; ?>name=<?php echo $row['name']; ?>">PRINT</a> <!-- Delete --></td>
</tr>
into this?
echo '<tr>';
echo '<td>' . $row['app_id'] . '</td>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>' . $row['addr'] . '</td>';
echo '<td>' . $row['contact'] . '</td>';
echo '<td>' . $row['curdate'] . '</td>';
echo 'PRINT CODE GOES HERE</td>';
You can try something like this :
$html = '<tr class="success">
<td>'. $row['app_id'].'</td>
<td>'.$row['name'].'</td>
<td>'.$row['addr'].'</td>
<td>'.$row['contact'].'</td>
<td>'.$row['curdate'].'</td>
<td><a style="font weight: 700; font-size:1.2em;" href="preview.php?app_id='.$row['app_id'].'name='.$row['name'].'">PRINT</a> <!-- Delete --></td>
</tr>';
echo $html;
You can try this :
echo '<tr class="success">
<td>'. $row['app_id'].'</td>
<td>'.$row['name'].'</td>
<td>'.$row['addr'].'</td>
<td>'.$row['contact'].'</td>
<td>'.$row['curdate'].'</td>
<td><a style="font weight: 700; font-size:1.2em;" href="preview.php?app_id='.$row['app_id'].'&name='.$row['name'].'">PRINT</a> </td>
</tr>';
once again, so "many ways to skin a cat"! (it's a Welsh idiom!)
currently I prefer this:
echo <<<HTML
<tr class="success">
<td>{$row['app_id']}</td>
<td>{$row['name']}</td>
<td>{$row['addr']}</td>
<td>{$row['contact']}</td>
<td>{$row['curdate']}</td>
<td><a style="font weight: 700; font-size:1.2em;" href="preview.php?app_id={$row['app_id']}&name={$row['name']}">PRINT</a> <!-- Delete --></td>
</tr>
HTML;
this way you don't have to worry about: dropping in and out of php (<?php, ?>); multiple echo statements; string concatenation with '. and .'; escaping any ' or " depending on which you used for your echo.

SQL query doesn't give the right result

I have a form where people can search the database for four values:
Location, Period, Day and Service. I always do not get the results that I want.
If I use AND, people need to fill in everything. If I use OR I get the complete database. I want to be able to search the database for those one to 4 things. Is there a way how I can do this?
Is there maybe a way to check which fields are filled in, and that the query is automatically changed with the filled in fields?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Zoeken</title>
</head>
<body>
<p><img src="add.png" width="20px" height="20px"/> | <img src="search.png" width="20px" height="20px"/> | <img src="number.png" width="20px" height="20px"/> </p>
<form action="" method="post">
<div>
<table>
<tr><td><strong>Locatie: </strong></td><td><input type="text" name="Locatie" value="" /></td> </tr>
<tr><td><strong>Periode: </strong></td><td><input type="text" name="Periode" value="" /></td> </tr>
<tr><td><strong>Dag: </strong></td><td><input type="text" name="Dag" value="" /></td> </tr>
<tr><td><strong>Dienst: </strong></td> <td><input type="text" name="Dienst" value="" /></td></tr>
<tr><td></td><td><input type="submit" name="zoeken" value="Zoeken"></td></tr>
</table>
</div>
</form>
<p><img src="add.png" width="20px" height="20px"/> | <img src="search.png" width="20px" height="20px"/> | <img src="number.png" width="20px" height="20px"/> </p>
</body>
</html>
<?php
if (isset($_POST['zoeken']))
{
include('connect-db.php');
$Locatie = $_POST['Locatie'];
$Periode = $_POST['Periode'];
$Dag = $_POST['Dag'];
$Dienst = $_POST['Dienst'];
// get results from database
$result = mysql_query("SELECT * FROM WMC_DeLijn WHERE Locatie='$Locatie' ANY Periode='$Periode' ANY Dag='$Dag'ANY Dienst='$Dienst' ")
or die(mysql_error());
// display data in table
echo "<h2>Resultaten:</h2><p>";
echo "<table border='1' cellpadding='10'>";
echo "<table><tr><th>ID</th><th>Locatie</th><th>Periode</th><th>Dag</th><th>Dienst</th><th>Delen</th><th>Geleed</th><th>Start 1</th><th>Eind 1</th><th>Start 2</th><th>Eind 2</th><th>Lijnen</th></tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {
// echo out the contents of each row into a table
echo "<tr>";
echo '<td align="center">' . $row['id'] . '</td>';
echo '<td align="center">' . $row['Locatie'] . '</td>';
echo '<td align="center">' . $row['Periode'] . '</td>';
echo '<td align="center">' . $row['Dag'] . '</td>';
echo '<td align="center">' . $row['Dienst'] . '</td>';
echo '<td align="center">' . $row['Delen'] . '</td>';
echo '<td align="center">' . $row['Geleed'] . '</td>';
echo '<td align="center">' . $row['Start1'] . '</td>';
echo '<td align="center">' . $row['Eind1'] . '</td>';
echo '<td align="center">' . $row['Start2'] . '</td>';
echo '<td align="center">' . $row['Eind2'] . '</td>';
echo '<td align="center">' . $row['Lijnen'] . '</td>';
//Link to edit record
echo '<td align="center"><img src="edit.png" width="20px" height="20px"/></td>';
// Link to delete record
echo '<td align="center"><img src="delete.png" width="20px" height="20px"/></td>';
//Link to Add Event to Google Calendar
echo '<td align="center"><img src="proceed.png" width="20px" height="20px"/></td>';
echo "</tr>";
}}
// close table>
echo "</table>";
?>
You can either build the query string dynamically, only adding WHERE clause statements if the parameter is not falsy, or add conditions in the SQL itself like so: WHERE (col = ? OR '' = ?) AND (col2 = ? OR '' = ?).
Change your query to have either a AND condition or a OR condition like
SELECT * FROM WMC_DeLijn
WHERE Locatie='$Locatie'
AND Periode='$Periode'
AND Dag='$Dag'
AND ienst='$Dienst';
(OR)
SELECT * FROM WMC_DeLijn
WHERE Locatie='$Locatie'
OR Periode='$Periode'
OR Dag='$Dag'
OR ienst='$Dienst';

How to make my table cell auto fit to contents in Bootstrap 3?

I am fetching data from Mysql database and populating them in a table.
However, i cannot seem to make the cell autofit to contents. I have tried width as the property of the table but i cant get it to work
Would really appreciate your help. Thanks
Here's what i have done so far
<div id="page-content-wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-lg-12">
<table class="table table-bordered" style="width:100%">
<thead>
<tr>
<th><center>ID</center></th>
<th>Name</th>
<th><center>Email</center></th>
<th>Number</th>
<th>Package</th>
<th>Flexibility</th>
<th >Date</th>
<th>Departuring From</th>
<th>Departure Date</th>
<th>Destination</th>
<th>Arrival Date</th>
<th>Price</th>
<th>Consolidator</th>
</tr>
</thead>
<tbody>
<?php
$query = 'SELECT * FROM queries';
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
echo '<tr>';
echo '<td>'. $row['id'] . '</td>';
echo '<td>'. $row['name'] . '</td>';
echo '<td>'. $row['email'] . '</td>';
echo '<td>'. $row['contactnumber'] . '</td>';
echo '<td>'. $row['packagedetails'] . '</td>';
echo '<td>'. $row['flexibility'] . '</td>';
echo '<td>'. $row['datetoday'] . '</td>';
echo '<td>'. $row['departure'] . '</td>';
echo '<td>'. $row['dateofdeparture'] . '</td>';
echo '<td>'. $row['destination'] . '</td>';
echo '<td>'. $row['dateofarrival'] . '</td>';
echo '<td>'. $row['price'] . '</td>';
echo '<td>'. $row['vendor'] . '</td>';
echo '<td width=250>';
echo '<a class="btn btn-success" href="readquery.php?id='.$row['id'].'">Read</a>';
echo ' ';
echo '<a class="btn btn-success" href="updatequery.php?id='.$row['id'].'">Update</a>';
echo ' ';
echo '<a class="btn btn-danger" href="deletequery.php?id='.$row['id'].'">Delete</a>';
echo '</td>';
echo '</tr>';
}
?>
</tbody>
</table>
I never see you echo </tr> or </td>. It would be helpful to give us an output of the HTML being generated by your while loop.
The problem was not with my table. To make it work, i increased the width of my container in which my table was inside, and it worked

Use PHP variable in href inside of html inside of php?

So I'm trying to make a cart for my website (nothing too special, uni project).
Currently I have
$product_id = $row["ID"];
$product_name = $row['name'];
$product_price = $row['price'];
$product_image = $row['img'];
$imgurl = ".\img\\".$product_image;
if ($i % 3 == 0) { // if $i is divisible by our target number (in this case "3")
$dyn_table .= '<tr><td align="center" valign="middle" width="350">'. "<img src=$imgurl width='100' hieght='100'> <br />" . $product_name . '<br /> &pound'.$product_price . '<br /><a href="./cart.php?product="$product_id"&action=add" />Add to Card</a></td>';
} else {
$dyn_table .= '<td align="center" valign="middle" width="350">'. "<img src=$imgurl width='100' hieght='100'> <br />" . $product_name . '<br /> &pound'.$product_price . '<a href="./cart.php?product=$product_id&action=add" />Add to Card</a></td>';
I need to have the product ID inserted in href where I have $product_id
<a href="./cart.php?product=$product_id&action=add" />Add to Card</a>
Currenly, it loads the links as domain.com/cart.php?product=$product_id&action=add
you can use variable name between quotes
provided if it is a single quoted you have to put {} around variable
else if it is a double quotes you can directly use the varibale
if ($i % 3 == 0) {
$dyn_table .= "<tr><td align='center' valign='middle' width='350'>". "<img src=$imgurl width='100' hieght='100'> <br />" . $product_name . "<br /> &pound".$product_price . "<br />
<a href='./cart.php?product={$product_id}&action=add' />Add to Card</a></td>";
} else {
$dyn_table .= "<td align='center' valign='middle' width='350'>". "<img src=$imgurl width='100' hieght='100'> <br />" . $product_name . "<br /> &pound".$product_price . "<a href='./cart.php?product={$product_id}&action=add' />Add to Card</a></td>";
}
The variable has to be extracted from the string, in PHP, so:
<a href="./cart.php?product='.$product_id.'&action=add" />Add to Card</a>
Like you did for your other variables.
this:
$dyn_table .= '<tr><td align="center" valign="middle" width="350">'."<img src=$imgurl width='100' hieght='100'><br />" . $product_name.'<br /> &pound'.$product_price .'<br /><a href="./cart.php?product=" .$product_id. "&action=add." />Add to Card</a></td>';
Your original problem was that you didn't jump out of single quotes properly to add $product_id in, however there's another instrumental problem in your code.
You should either use all '' single quotes or all "" double quotes, not a combination of both. It makes it hard to read and is unnecessary regardless.
Therefore, it should be either
$dyn_table .= '<tr><td align="center" valign="middle" width="350"><img src="' . $imgurl . '" width="100" hieght="100"> <br />' . $product_name . '<br /> £' . $product_price . '<br /><a href="./cart.php?product=' . $product_id . '&action=add" />Add to Card</a></td>';
or
$dyn_table .= "<tr><td align=\"center\" valign=\"middle\" width=\"350\"><img src=\"$imgurl\" width=\"100\" hieght=\"100\"> <br />$product_name<br /> £$product_price<br /><a href=\"./cart.php?product=$product_id&action=add\" />Add to Card</a></td>";
In this case, I'd probably use single quotes so I don't have to escape all the double quotes in the HTML, or perhaps even a heredoc, but I won't get into those for now.
Your code for single quotes would be
if ($i % 3 == 0) { // if $i is divisible by our target number (in this case "3")
$dyn_table .= '<tr><td align="center" valign="middle" width="350"><img src="' . $imgurl . '" width="100" height="100"> <br />' . $product_name . '<br /> £' . $product_price . '<br /><a href="./cart.php?product=' . $product_id . '&action=add" />Add to Card</a></td>';
} else {
$dyn_table .= '<td align="center" valign="middle" width="350"><img src="' . $imgurl . '" width="100" hieght="100"> <br />' . $product_name . '<br /> £' . $product_price . '<a href="./cart.php?product=' . $product_id . '&action=add" />Add to Card</a></td>';
I'd suggest you look up heredocs though, you may find those easier.

Categories