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>';
}
Related
I'm in the process of making a PHP website, where a user can input data, then search through it later, but I can't seem to organize the data :/
Heres my code:
<form action="uploads.php" method="GET"><input id="search" type="text" placeholder="Type here"><input id="submit" type="submit" value="Search"></form></body></html>
When a user searched for their name, it results as so:
firstname=Mickey lastname=Mouse item1= item2= item3= item4= item5= item6=
Is there any way I can add a CSS or something to get the entries to line break or seperate?
In Your display page while displaying the data you can use the <br> tag so that it will display each and every data in different line.
First Name: <?php echo $loopvariable['fname'].'<br />'; ?>
Last Name: <?php echo $loopvariable['lname'].'<br />'; ?>
Like this you can provide for all the data which you print.
Output:
First Name: Name One
Last Name: Name Two
And you can provide as such information using the break tags in separate lines.
Basically in PHP you can echo html code so you can echo <br> statements as in the previous answer you can also echo css and you can create a block of code that you include
<?php
include('SomeMoreCode.php');
?>
You can also echo formatting commands to create a table. Just place the html statements in'' and join html and mysql/php values with .
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>date</th> <th>Home Team</th> <th></th><th></th><th>Away Team</th> <th></th> </tr>";
while($row = mysql_fetch_array( $result )) {
echo "<tr>";
echo '<td>' . $row['date'] . '</td>';
echo '<td>' . $row['hometeam'] . '</td>';
echo '<td>' . $row['fthg'] . '</td>';
echo '<td>' . $row['ftag'] . '</td>';
echo '<td>' . $row['awayteam'] . '</td>';
echo '<td>Edit</td>';
echo "</tr>";
}
echo "</table>";
non mysql - php only
<?php
$date='19/6/16';
$hometeam='Man United';
$fthg='3';
$ftag='2';
$awayteam='Man City';
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>date</th> <th>Home Team</th> <th></th><th></th><th>Away Team</th> </tr>";
echo "<tr>";
echo '<td>' . $date . '</td>';
echo '<td>' . $hometeam . '</td>';
echo '<td>' . $fthg . '</td>';
echo '<td>' . $ftag . '</td>';
echo '<td>' . $awayteam . '</td>';
echo "</tr>";
echo "</table>";
?>
Please help cant display image when I use php
$result = mysql_query("SELECT * FROM obs") or die(mysql_error());
echo "<center><table><tr><th width=200>Obs Number</th><th width=200>Name</th><th width=200>Purpose</th><th width=200>Date Of OB</th><th width=200>Destination</th><th width=200>Image</th></tr></center>";
while ($row = mysql_fetch_array($result)) {
echo '<tr>';
//cant display image
echo '<tr> <input type="hidden" name="showimage[]" value="'. $row['OBS_NUMBER'].'" >';
echo '<td width=200>' . $row['OBS_NUMBER'] . '</td>';
echo '<td width=200>' . $row['NAME'] . '</td>';
echo '<td width=200>' . $row['PURPOSE'] . '</td>';
echo '<td width=200>' . $row['DATE_OF_OB'] . '</td>';
echo '<td width=200>' . $row['DESTINATION'] . '</td>';
echo '<td width=200> <img src="data:image/jpeg;base64,' . base64_encode($row['imagesobs']) . '" width="250"></td>';
echo '</tr>';
}
I have extracted hundreds of data from MYSQL database then populated them to a table. Each table has a column name "Preview" which will generate preview button. When we click on that preview button it has to pass Application ID of that row to genpdf.php but I could pass the Application ID of that specific row.
<form id="genpdf" action="genpdf.php" method="POST">
<h3 style="padding:10px;">List of Application Submitted</h3>
<table width="100%" style="padding: 10px;">
<tr>
<td><strong>S. No.</strong></td>
<td><strong>Application ID</strong></td>
<td><strong>Name</strong></td>
<td><strong>Date of Birth</strong></td>
<td><strong>Telephone</strong></td>
<td><strong>Email</strong></td>
<td><strong>Preview</strong></td>
</tr>
<?php
$i = 1;
while($row = mysql_fetch_array($record))
{
echo '<tr><td>' . $i . '</td>';
echo '<td>' . $row['applicationid'] . '</td>';
echo '<td>' . $row['title'] . ' ' . $row['firstname'] . ' ' . $row['middlename'] . ' ' . $row['familyname'] . '</td>';
echo '<td>' . $row['dobmonth'] . '/' . $row['dobday'] . '/' . $row['dobyear'] . '</td>';
echo '<td>' . $row['telephonet4'] . '</td>';
echo '<td>' . $row['emailt4'] . '</td>';
echo '<td><input type="submit" value "Preview" /></td>';
$i += 1;
}
?>
</table>
</form>
You have to replace the form submit button with a regular link that calls the genpdf.php page and adds the applicationId as a HTTP GET parameter.
Preview
In PHP MYSQL_FETCH_ASSOC is omitting Last Row. This never happened. But this time it put me into soup at the last moment.
Even I've put up mysql_num_rows the result is 14 records -- but on list it shows only 13 records and the 14th record is omitted.
Any kind of help is Appreciated.
$uno = $_GET["uno"];
$xtc1 = 'select * from rform where uno="' . $uno . '" order by rno DESC';
$xtc = mysql_query($xtc1) or die('User Reservation Retrival Error : ' . mysql_error());
$trno = mysql_fetch_assoc($xtc);
$trow = mysql_num_rows($xtc);
echo '<p>List of Onlilne Reservations made by <strong style="font-weight:bold; color:red;">' . ucwords($trno["cname"]) . ' (' . $trow . ')</strong></p>';
echo '<table cellpadding="5" cellspacing="0" border="1">';
echo '<tr>';
echo '<td colspan="5" style=" font-size:14px; text-align:center; font-weight:bold; color:red;">' . ucwords($trno["cname"]) . '</td>';
echo '</tr>';
echo '<tr>';
echo '<th>R.NO</th>';
echo '<th>From</th>';
echo '<th>To</th>';
echo '<th>Date & Time of<Br>Travel</th>';
echo '<th>Reserved On</th>';
echo '</tr>';
while($mtn = mysql_fetch_assoc($xtc)){
$dt = $mtn["csdate"] . ' ' . $mtn["ctime"];
echo '<tr>';
echo '<td>' . $mtn["rno"] . '</td>';
echo '<td>' . $dt . '</td>';
echo '<td>' . $mtn["caddr"] . '</td>';
echo '<td>' . $mtn["cdest"] . '</td>';
echo '<td>' . date('d-M-Y',strtotime($mtn["tstamp"])) . '</td>';
echo '</tr>';
}
echo '</table>';
You have an extra $trno = mysql_fetch_assoc($xtc) that you sem to be discarding. This is your missing row. Just remove that line.
deleting the first $trno = mysql_fetch_assoc($xtc); will solve this problem.
In case you need to read the first line of $xtc before the loop. You can change while loop to do-while, without deleted the first $mtn = mysql_fetch_assoc($xtc);
do{
//whatever
}while($mtn = mysql_fetch_assoc($xtc));
I am looking for simple, the best practical way of placing summed value above HTML table.
Normally loops can generate sums after the loop is finished that is below the table of all results. It may be very difficult when the table is very long and we need to scroll down every time to see the sum of sales. Do I need to run two loops or is there any trick to do that? Task seems very simple. In below code I am summing Quantities from all orders and Total Sales values:
$lp=1; $total=0; $total_qt=0;
while ($record = mysql_fetch_array($result)){
$total += $record['Total'];
$total_qt += $record['Qt'];
echo '<tr style="background-color:'. ((next($colour) == true) ? current($colour) : reset($colour)).'">
<td> ' . $lp++ . '</td>
<td class="bolder"> ' . $record['Name'] . '</td>
<td> ' . $record['Product'] . '</td>';
if ($invoice=="on") echo '<td style="font-size:11px">' . $record['Invoice'] . '</td>';
echo '<td> ' . $record['despatched'] . '</td>
<td> ' . $record['Qt'] . '</td>
<td> ' . $record['Price'] . '</td>
<td class="align_right"> ' . $record['Total'] . '</td>';
echo '</tr>';
}
echo '</table><div class="stat_total">Qt: '.$total_qt.' Total: £'.$total.'</div> </div>';
You could use a variable to store your output while you are in the loop, rather than echo it, and when you finished the loop then echo the stored results with both totals before and after them. Like this:
$lp=1; $total=0; $total_qt=0;
$output = ''; //here you temporarily save your output
while ($record = mysql_fetch_array($result)){
$total += $record['Total'];
$total_qt += $record['Qt'];
$output .= '<tr style="background-color:'. ((next($colour) == true) ? current($colour) : reset($colour)).'">
<td> ' . $lp++ . '</td>
<td class="bolder"> ' . $record['Name'] . '</td>
<td> ' . $record['Product'] . '</td>';
if ($invoice=="on") $output .= '<td style="font-size:11px">' . $record['Invoice'] . '</td>';
$output .= '<td> ' . $record['despatched'] . '</td>
<td> ' . $record['Qt'] . '</td>
<td> ' . $record['Price'] . '</td>
<td class="align_right"> ' . $record['Total'] . '</td>';
$output .= '</tr>';
}
//Now you can put a div with the Total both at the start and end of the table
echo '<div class="stat_total">Qt: '.$total_qt.' Total: £'.$total.'</div>';
echo $output . '</table>';
echo '<div class="stat_total">Qt: '.$total_qt.' Total: £'.$total.'</div> </div>';
By using the SUM() SQL function you'll be able to access the sums without needing to run PHP:
SQL
SELECT SUM(Total) FROM Orders