I'm new to php and I'm working with a tutorial about showing images dynamically on the page, it works fine but it shows them vertically and I would like them to be horizontal. I created a page with code to do that but I can't seem to figure out where to insert the code to get the images to show.
Thanks for any help.
Vertical output looks like this
$dynamicList = "";
$sql = mysql_query("SELECT * FROM products ORDER BY date_added ASC LIMIT 6");
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$product_name = $row["product_name"];
$price = $row["price"];
$details = $row["details"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
$dynamicList .= '<table width="1000px" border="0" cellspacing="0" cellpadding="6" align="center">
<tr>
<td width="1000px" align="center"><img style="border:#666 0px solid;" src="images/' . $id . '.jpg" width="50%" height="50%" alt="' . $product_name . '" width="77" height="102" border="1" /></td>
<td width="83%" valign="top">' . $product_name . '<br />
$' . $price . '<br /> $' . $details . '<br />
order</td>
</tr>
</table>';
}
mysql_close();
?>
Grid Output
$sql = mysql_query("SELECT * FROM products ORDER BY id ASC LIMIT 15");
$i = 0;
// Establish the output variable
$dynamiclist = '<table width="1000px" border="1" cellspacing="2" cellpadding="10" align="center">';
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$product_name = $row["product_name"];
$details = $row["details"];
$price = $row["price"];
if ($i % 3 == 0) { // if $i is divisible by our target number (in this case "3")
$dynamiclist .= '<tr><td>' . $product_name . '</br>' . $details . '</br>' . $price . '</td>';
} else {
$dynamiclist .= '<td>' . $product_name . '</td>';
}
$i++;
}
$dynamiclist .= '</tr></table>';
?>
I got it figured out, Thanks for the help.
$sql = mysql_query("SELECT * FROM products ORDER BY id ASC LIMIT 15");
$i = 0;
// Establish the output variable
$dynamiclist = "";
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$product_name = $row["product_name"];
$details = $row["details"];
$price = $row["price"];
if ($i % 4 == 0) { // if $i is divisible by our target number (in this case "3")
$dynamiclist .= '<tr><td width="250px" align="center"><img src="images/' . $id . '.jpg"><br/>' . $product_name . '<br />
' . $details . '<br /> $' . $price . '<br />
order</td>';
} else {
$dynamiclist .= '<td width="250px" align="center"><img src="images/' . $id . '.jpg"><br/>' . $product_name . '<br />
' . $details . '<br /> $' . $price . '<br />
order</td>';
}
$i++;
}
$dynamiclist .= '</tr></table>';
?>
Related
I have search and implements from many question in stackoverflow to my code but it always returning only the last row value.
Here my code :
$totalpayment = 0;
foreach($respon2 as $key => $row) {
$name = $row['title'];
$quantity = $row['quantity'];
$price = $row['price']*$row['quantity'];
$totalpayment += $price;
$price = '$ ' . number_format($price) ;
$totalpayment = '$ ' . number_format($totalpayment) ;
$mytable .= '<tr width="100%">';
$mytable .= '<td width="55%" align="left">' . $name . "</td>";
$mytable .= '<td width="20%" align="right">' . $quantity . "</td>";
$mytable .= '<td width="25%" align="right">' . $price . "</td>";
$mytable .= '</tr>';
}
$mytable .= '</table>';
$mytable .= '<table border="0" cellpadding="10" cellspacing="0" width="100%%" style="padding: 0px 25px 0px 25px;">';
$mytable .= '<tr><td width="45%"></td><td width="30%" class="table-title" align="right">Total Payment:</td><td width="25%" align="right">'. $totalpayment ."</td></tr>";
$mytable .= '</table>';
How to fix this ?
You are overding $totalpayment on every loop by this code:
$totalpayment = '$ ' . number_format($totalpayment) ;
$totalpayment = 0;
foreach($respon2 as $key => $row) {
$name = $row['title'];
$quantity = $row['quantity'];
$price = $row['price']*$row['quantity'];
$totalpayment += $price;
$price = '$ ' . number_format($price) ;
$mytable .= '<tr width="100%">';
$mytable .= '<td width="55%" align="left">' . $name . "</td>";
$mytable .= '<td width="20%" align="right">' . $quantity . "</td>";
$mytable .= '<td width="25%" align="right">' . $price . "</td>";
$mytable .= '</tr>';
}
$totalpayment = '$ ' . number_format($totalpayment); //Try transferring this code outside the loop
$mytable .= '</table>';
$mytable .= '<table border="0" cellpadding="10" cellspacing="0" width="100%%" style="padding: 0px 25px 0px 25px;">';
$mytable .= '<tr><td width="45%"></td><td width="30%" class="table-title" align="right">Total Payment:</td><td width="25%" align="right">'. $totalpayment ."</td></tr>";
$mytable .= '</table>';
It is not so efficient to do a sum in loop, better change your code as follows and to be done out side the loop as mentioned in question.
$prices = sum(array_coulmn($respon2, 'price'));
$quantity = sum(array_coulmn($respon2, 'quantity'));
$totalPayment = $prices*$quantity;
$totalPayment = '$ ' . number_format($totalPayment);
$totalpayment = 0;
$mytable ='<table>';
$respon2=array(array('title' =>'p1','quantity' =>2,'price' =>10),array('title' =>'p2','quantity' =>2,'price' =>20),array('title' =>'p3','quantity' =>1,'price' =>10));
foreach($respon2 as $key => $row) {
$name = $row['title'];
$quantity = $row['quantity'];
$price = $row['price']*$row['quantity'];
$totalpayment += $price;
$price = '$ ' . number_format($price) ;
$mytable .= '<tr width="100%">';
$mytable .= '<td width="55%" align="left">' . $name . "</td>";
$mytable .= '<td width="20%" align="right">' . $quantity . "</td>";
$mytable .= '<td width="25%" align="right">' . $price . "</td>";
$mytable .= '</tr>';
}
$totalpayment = '$ ' . number_format($totalpayment) ;
$mytable .= '</table>';
$mytable .= '<table border="0" cellpadding="10" cellspacing="0" width="100%%" style="padding: 0px 25px 0px 25px;">';
$mytable .= '<tr><td width="45%"></td><td width="30%" class="table-title" align="right">Total Payment:</td><td width="25%" align="right">'. $totalpayment ."</td></tr>";
$mytable .= '</table>';
echo $mytable;
I would like to display the results of this search in a table, would anyone be able to tell me how I could achieve such a thing please? I got this code from a tutorial website but alas it does not tell me how to get my results in a four column table. Such as this layout.
|Picture|Name|Description|Price|
<?php
$conn = mysqli_connect("localhost", "root", "", "catalog");
if(mysqli_connect_errno()){
echo "Failed to connect: " . mysqli_connect_error();
}
error_reporting(0);
$output = '';
if(isset($_GET['q']) && $_GET['q'] !== ' '){
$searchq = $_GET['q'];
$q = mysqli_query($conn, "SELECT * FROM final_dog_catologue_full WHERE name LIKE '%$searchq%' OR brand LIKE '%$searchq%'") or die(mysqli_error());
$c = mysqli_num_rows($q);
if($c == 0){
$output = 'No search results for <b>"' . $searchq . '"</b>';
} else {
while($row = mysqli_fetch_array($q)){
$Name = $row['Name'];
$brand = $row['Brand'];
$picture = $row['Picture'];
$description = $row['description'];
$Retail_Price_With_Delievery = $row['Price'];
$output .= '<a href="' . $brand . '">
<h3>' . $brand . '</h3>
<p>'. $brand .'</p>
</a>';
}
}
} else {
header("location: ./");
}
print("$output");
mysqli_close($conn);
?>
To display the values into a table using HTML, just modify the $output variable
$output .= '<tr>
<td>' . $picture. '</td>
<td>' . $name . '</td>
<td>'. $description .'</td>
<td>'. $price .'</td>
</tr>';
You would need to add the "table" tags before and after while loop
else {
$output = '<table><tr>
<th>' . $picture. '</th>
<th>' . $name . '</th>
<th>'. $description .'</th>
<th>'. $price .'</th>
</tr>';
while($row = mysqli_fetch_array($q)){...}
$output .= '</table>';
I've read about the nl2br() command but they echo out the variable to make it work but my variable is inside a list that I echo out.
Should I somehow put the nl2br() command in my if statement where I'm setting up the output or should I be looking somewhere else for my answer.
I have made 2 extra detail variables to output so I get 3 lines of text for the description of my product but there has to be a better way of doing it than that. I would think 1 detail variable with all the info would be the preferred way of doing it
I have tried to search for it but I'm afraid I'm just not asking the right question so any help in the right direction is appreciated.
<?php require_once './connections/connect_mysqli.php';
$conn = dbConnect('read');
$sql = "SELECT * FROM products ORDER BY id ASC LIMIT 6 ";
$result = $conn->query($sql) or die($conn->error);
$i = 0;
$flatlist = "";
while($row = $result->fetch_array(MYSQLI_ASSOC)){
$id = $row["id"];
$product_name = $row["product_name"];
$details = $row["details"];
$details2 = $row["details2"];
$details3 = $row["details3"];
$price = $row["price"];
if ($i % 4 == 0) {
$flatlist .='<tr><td width="250px" align="center"><img src="images/' . $id . '.jpg"><br/>
<p style=color:blue font-size=14px;>' . $product_name . '</p><br/>
<p style=font-size:14px;>' . $details . '</p><br />
' . $details2 . '<br />
' . $details3 . '<br />
<p style=font-size:14px;>$' . $price . '</p><br />
<input type="button" value="Order" style=color:blue></td>';
} else {
$flatlist .= '<td width="250px" align="center"><img src="images/' . $id . '.jpg"><br/>
<p style=color:blue font-size=14px;>' . $product_name . '</p><br />
<p style=font-size:14px;>' . $details . '</p><br />
' . $details2 . '<br />
' . $details3 . '<br />
<p style=font-size:14px;>$' . $price . '</p><br />
<input type="button" value="Order" style=color:blue></td>';
}
$i++;
}
$flatlist .= '</tr></table>';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Flats</title>
</head>
<body>
<?php require 'includes/skyline.php'; ?>
<?php require 'includes/menu.php'; ?>
<table width="1200" border="0" align="center">
<tr>
<?php echo $flatlist ?>
</tr>
</table>
<?php require 'includes/footer.php';?>
Want to point out you close the details <p> before the 2nd and 3rd lines. You probably want to close that after third line.
<p style=font-size:14px;>' . $details . '<!-- remove here: </p> --><br />
' . $details2 . '<br />
' . $details3 . '<br /><!-- add here: --></p>
If you keep your same database structure, you can define into one variable after retrieving like:
$details = $row["details"] . '<br/>' . $row['details2'] . '<br/>' . $row['details3'];
And then just refer back to $details later on.
Why don't you want to use nl2br()? This would allow you to store multiple lines all into details column. Then you would just use:
$details = nl2br( $row["details"] );`
Here are my edits to your code:
<?php require_once './connections/connect_mysqli.php';
$conn = dbConnect('read');
$sql = "SELECT * FROM products ORDER BY id ASC LIMIT 6 ";
$result = $conn->query($sql) or die($conn->error);
$i = 0;
$flatlist = "";
while($row = $result->fetch_array(MYSQLI_ASSOC)){
$id = $row["id"];
$product_name = $row["product_name"];
$details = $row["details"] . '<br/>' . $row['details2'] . '<br/>' . $row['details3'];
$price = $row["price"];
if ($i % 4 == 0) {
$flatlist .='<tr><td width="250px" align="center"><img src="images/' . $id . '.jpg"><br/>
<p style=color:blue font-size=14px;>' . $product_name . '</p><br/>
<p style=font-size:14px;>' . $details . '</p><br />
<p style=font-size:14px;>$' . $price . '</p><br />
<input type="button" value="Order" style=color:blue></td>';
} else {
$flatlist .= '<td width="250px" align="center"><img src="images/' . $id . '.jpg"><br/>
<p style=color:blue font-size=14px;>' . $product_name . '</p><br />
<p style=font-size:14px;>' . $details . '</p><br />
<p style=font-size:14px;>$' . $price . '</p><br />
<input type="button" value="Order" style=color:blue></td>';
}
$i++;
}
how can I enter only positive number and if I entered negative number it will convert automatically to positive number this the code That I have how can I edit it
<?php
if (isset($_SESSION["cart_products"]) && count($_SESSION["cart_products"]) > 0) {
echo '<div class="cart-view-table-front" id="view-cart">';
echo '<h3>Your Shopping Cart</h3>';
echo '<form method="post" action="cart_update.php">';
echo '<table width="100%" cellpadding="6" cellspacing="0">';
echo '<tbody>';
$total = 0;
$b = 0;
foreach ($_SESSION["cart_products"] as $cart_itm) {
$product_name = $cart_itm["product_name"];
$product_qty = $cart_itm["product_qty"];
$product_price = $cart_itm["product_price"];
$product_code = $cart_itm["product_code"];
$product_color = $cart_itm["product_color"];
$bg_color = ($b++ % 2 == 1) ? 'odd' : 'even'; //zebra stripe
echo '<tr class="' . $bg_color . '">';
echo '<td>Qty <input type="text" size="3" maxlength="3" name="product_qty[' . $product_code . ']" value="' . $product_qty . '" /></td>';
echo '<td>' . $product_name . '</td>';
echo '<td><input type="checkbox" name="remove_code[]" value="' . $product_code . '" /> Remove</td>';
echo '</tr>';
$subtotal = ($product_price * $product_qty);
$total = ($total + $subtotal);
}
echo '<td colspan="4">';
echo '<button type="submit">Update</button>Checkout';
echo '</td>';
echo '</tbody>';
echo '</table>';
$current_url = urlencode($url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
echo '<input type="hidden" name="return_url" value="' . $current_url . '" />';
echo '</form>';
echo '</div>';
}
?>
GrumpyCrouton > What is the variable that you are trying to convert to positive?
M.Alhaddad > #GrumpyCrouton product_qty
It's quite simple.
Just use abs() on the variable.
$product_qty = abs($cart_itm["product_qty"]);
Note: abs() works in (PHP 4, PHP 5, PHP 7)
"Converting a number to positive" is just getting it's absolute value.
Documentation:
PHP function.abs
I'm very new to working with php / mysql coding, and I've got a slight problem with displaying results that are linked to an sql database.
At the moment people search using the below code - which works fine - it's a drop down select box, but you can only select one option. I want it so the items are check boxes and you can select more than one item.
I've included below the code that's used for when people input the data to the database - which are check boxes - and I have tried replacing the 'drop down select code' with this but it doesn't work.
Does anybody know what code I have to use to replace the 'drop down select code' so that checkboxes are viewable and you can filter more than one item - I have also included the 'results page code', which displays the results and I'm thinking that 'ClientStage' needs adding to the 'check box code' somewhere.
Sorry about my lack of knowledge with this and would be grateful for some help?
DROP DOWN SELECT CODE
<select name="ClientStage" id="ClientStage">
<option value=""></option>
<?php
include 'Easyspace.php';
$sql = 'SELECT * FROM `clienttype`;';
$rs = mysql_query($sql, $conn) or die ("error with sql query ".$sql);
while ($row = mysql_fetch_array ($rs)){
$ClienttypeID = $row["ClienttypeID"];
$Clienttype = $row["Clienttype"];
echo '<option value="' .$ClienttypeID. '">' .$Clienttype. '</option>';
}
?>
</select></span>
CHECK BOX CODE
<table width="100%" border="0" cellspacing="1">
<?php
$side=1;
$sql = 'SELECT * FROM `clienttype`;';
$rs = mysql_query($sql, $conn) or die ("error with sql query ".$sql);
while ($row = mysql_fetch_array ($rs)){
$ClienttypeID = $row["ClienttypeID"];
$Clienttype = $row["Clienttype"];
if ($side == 1){
$side = 2;
echo '<tr>';
echo '<td><span class="RPATtext"><input type="checkbox" name="Clients[]" value="' .$ClienttypeID. '"> ' .$Clienttype. '</div></td>';
} else {
$side = 1;
echo '<td><span class="RPATtext"><input type="checkbox" name="Clients[]" value="' .$ClienttypeID. '"> ' .$Clienttype. '</div></td>';
echo '<option value="' .$ClienttypeID. '">' .$Clienttype. '</option>';
}
}
?>
</table>
RESULTS PAGE CODE
<?php
$Country = $_POST['Country'];
$County = $_POST['County'];
$ClientStage = $_POST['ClientStage'];
$HealthIssues = $_POST['HealthIssues'];
include 'Easyspace.php';
$sql = "SELECT * FROM `therapists` WHERE ";
if ($Country){
$sql .= "`Country` = '$Country'";
}
if ($County){
if ($Country){
$sql .= ' AND ';
}
$sql .= "`County` = '$County'";
}
if ($ClientStage){
if ($Country or $County){
$sql .= ' AND ';
}
$sql .= "FIND_IN_SET('$ClientStage', Client_typeID)";
}
if ($HealthIssues){
if ($Country or $County or $ClientStage){
$sql .= ' AND ';
}
$sql .= "FIND_IN_SET('$HealthIssues', IssuesID)";
}
// echo $sql;
$rs = mysql_query($sql, $conn) or die ("error with sql query ".$sql);
while ($row = mysql_fetch_array ($rs)){
$TherapistID = $row['TherapistID'];
$Title = $row['Title'];
$FirstName = $row['First Name'];
$LastName = $row['Last Name'];
$PostCode = $row['PostCode'];
echo '<tr>';
echo '<td valign="middle" bgcolor="#D9E5C3" class="bodycopy">' .$Title. '</td>';
echo '<td valign="middle" bgcolor="#D9E5C3" class="bodycopy">' .$FirstName. '</td>';
echo '<td valign="middle" bgcolor="#D9E5C3" class="bodycopy">' .$LastName. '</td>';
echo '<td valign="middle" bgcolor="#D9E5C3" class="bodycopy">' .$PostCode. '</td>';
echo '<td height="34" valign="middle" bgcolor="#D9E5C3"><p class="bodycopy">View more details</p></td>';
echo '</tr>';
}
?>
Here's your new results code. I hope it's what you were looking for. Pay attention to your code: try to puts vars in lowercase ($Country has to be $country, try to use PDO for MySQL, etc...).
<?php
$Country = $_POST['Country'];
$County = $_POST['County'];
/* change here */
$ClientStage = $_POST['Clients'];
$HealthIssues = $_POST['HealthIssues'];
include 'Easyspace.php';
$sql = "SELECT * FROM `therapists` WHERE ";
if ($Country){
$sql .= "`Country` = '$Country'";
}
if ($County){
if ($Country){
$sql .= ' AND ';
}
$sql .= "`County` = '$County'";
}
if ($ClientStage){
if ($Country or $County){
$sql .= ' AND ';
}
/* change here */
$sql .= 'Client_typeID IN (';
foreach($ClientStage as $i => $id) {
if ($i !== 0)
$sql .= ',';
$sql .= "'" . $id . '"';
}
$sql .= ')';
}
if ($HealthIssues){
if ($Country or $County or $ClientStage){
$sql .= ' AND ';
}
$sql .= "FIND_IN_SET('$HealthIssues', IssuesID)";
}
// echo $sql;
$rs = mysql_query($sql, $conn) or die ("error with sql query ".$sql);
while ($row = mysql_fetch_array ($rs)){
$TherapistID = $row['TherapistID'];
$Title = $row['Title'];
$FirstName = $row['First Name'];
$LastName = $row['Last Name'];
$PostCode = $row['PostCode'];
echo '<tr>';
echo '<td valign="middle" bgcolor="#D9E5C3" class="bodycopy">' .$Title. '</td>';
echo '<td valign="middle" bgcolor="#D9E5C3" class="bodycopy">' .$FirstName. '</td>';
echo '<td valign="middle" bgcolor="#D9E5C3" class="bodycopy">' .$LastName. '</td>';
echo '<td valign="middle" bgcolor="#D9E5C3" class="bodycopy">' .$PostCode. '</td>';
echo '<td height="34" valign="middle" bgcolor="#D9E5C3"><p class="bodycopy">View more details</p></td>';
echo '</tr>';
}
?>