I would like to put status on item. When the quantity is 0 it should come out Sold Out else Available. But I'm getting a parse error on line 16. Why is that?
<?php
// Connect to the MySQL database
include "storescripts/connect_to_mysql.php";
$dynamicList = "";
$status = "";
$sql = mysql_query("SELECT * FROM products ORDER BY date_added");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$product_name = $row["product_name"];
$price = $row["price"];
$quantity = $row["quantity"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
$status = if($quantity == 0) echo "Sold Out";
else echo "Available";
$dynamicList .= '<table width="100%" border="0" cellspacing="1" cellpadding="6">
<tr>
<td width="17%" valign="top"><img style="border:#666 0px solid;" src="inventory_images/' . $id . '.jpg" alt="' . $product_name . '" width="77" height="102" border="1" /></td>
<td width="83%" valign="top">
' . $product_name . '<br />
$' . $price . '<br />
Quantity =' .$quantity . '
<br /> Status = ' . $status . ' <br />
</td>
</tr>
</table>';
}
} else {
$dynamicList = "We have no products listed in our store yet";
}
mysql_close();
?>
You have syntax errors in your code : you can't assign value to the if statement while echoing something in it.Try the following :
$status = $quantity == 0 ? "Sold Out" : "Available";
echo $status;
This is completely wrong syntax-wise:
$status = if($quantity == 0) echo "Sold Out";
else echo "Available";
Try this instead:
if ($quantity == 0)
$status = "Sold Out";
else
$status = "Available";
echo $status;
Related
//Also how can I achieve a 4x4 grid view of my products without using a table? thanks in advance.
<?php
include"storescripts/connect_to_mysql.php";
$sql=mysql_query("SELECT * FROM products ORDER BY date_added DESC LIMIT 15");
$columncount = 0;
$dynamicList = '<table align="center" width="100%" border="0" cellpadding="6"><tr>';
while($row=mysql_fetch_array($sql)){
$id=$row["id"];
$product_image = $row["product_image"];
$product_name = $row["product_name"];
$price=$row["price"];
$date_added = strftime("&b &d, &y", strtotime($row["date_added"]));
$dynamicList .= '<td width="135"><a href="product.php?id=' . $id . '">
<img src="inventory_images' . $id . '.jpg" alt="" width="129" height="169" border="0">
</a>
</td>
<td width="593" valign="bottom"></br>' . $product_name . '<br>
£' . $price . '<br>
View Product Details</td>';
//The missing table tag is causes my footer to go up above the product list. thats why I have left it out
if($columncount == 3)
{
$dynamicList .= '</tr>';
$columncount = 0;
}
else
{
$columncount++;
}
}
mysql_close();
?>
This question is more about HTML and CSS than about PHP. But a grid without using a HTML table you could do like this:
echo '<div class="items">' , PHP_EOL , PHP_EOL;
$itemCount = 0;
while($itemCount < 16){
$rowOpeningHtml = null;
$rowClosingHtml = null;
if($itemCount%4 === 0) {
$rowOpeningHtml = '<div class="item-row">' . PHP_EOL;
if($itemCount > 0)
$rowClosingHtml = '</div>' . PHP_EOL . PHP_EOL;
}
echo $rowClosingHtml , $rowOpeningHtml , "\t" , '<div class="item">item' , ($itemCount + 1) , '</div>' , PHP_EOL;
++$itemCount;
}
echo '</div>' , PHP_EOL , PHP_EOL , '</div>';
And style it with some fancy CSS.
I'm new to this so please be gentle
I want to style for example $product_name, $author, $details.
I guess I could style the echos but in this case the only echo is
<?php
// Connect to the MySQL database
include "storescripts/connect_mysql.php";
// This block grabs the whole list for viewing
$dynamicList = "";
$sql = mysql_query("SELECT * FROM products ORDER BY id DESC");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$product_name = $row["product_name"];
$author = $row["author"];
$details = $row["details"];
$link = $row["link"];
$isbn = $row["isbn"];
$isbn13 = $row["isbn13"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
$dynamicList .= '<table width="580" border="0" cellspacing="0">
<tr>
<td width="120"><img src="/inventory_images/' . $id . '.jpg" width="120" height="184" border="0" /></td>
<td width="456"><p>' . $product_name . '<p><br />
<p>' . $author . '</span><br />
<p>' . $details . '<br />
visa </td>
</tr>
</table>';
}
} else {
$dynamicList = "Database empty.";
}
mysql_close();
?>
Have you tried adding some CSS classes to your table cells?
$dynamicList .= '
<table width="580" border="0" cellspacing="0">
<tr>
<td width="120"><img src="/inventory_images/' . $id . '.jpg" width="120" height="184" border="0" /></td>
<td width="456"><p class="product_name">' . $product_name . '<p><br />
<p class="author">' . $author . '</span><br />
<p class="detail">' . $details . '<br />
visa </td>
</tr>
</table>';
In the head add some styles:
<style>
.product_name{color:red;}
.author{color:green;}
.detail{color:blue;}
</style>
Why not just add CSS rules to your stylesheet?
table tr td p {
// some styles
}
You can add a class with styles to the <p> elements that contain each value
<td width="456"><p class="product-name">' . $product_name . '</p>...
PHP is only a preprocssor and coughs up the code as the page loads and before any of the css of HTML kicks in so you can style it as normal
You can use style="" and class="" parameters on your <p> paragraph tags as well on the table cells <td>. You can also try to add for example styled <span> or <div> tags to your variables I suppose.
For example:
$product_name = "<span class='yourstyleclass'>$row['product_name']</span>";
The code given below which display all the products in the home page.. and each product has a checkbox.. I wanted to select the checkbox of few products, and if I click on submit, the next page doesn't view the selected checkbox results
$sql = mysql_query("SELECT * FROM products ORDER BY date_added DESC LIMIT 15");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
$colindex = 1;
$totcols = 3;
$rowclosed = false;
//$totrows=ceil($count/$totcols);
$dynamicList .= "<dl id='Searchresult'> <form action='selected.php' method='POST'> <table width=\"50%\" border=\"0\" cellspacing=\"0\" align=\"center\" cellpadding=\"3\">";
while ($row = mysql_fetch_array($sql)) {
$id = $row["id"];
$product_name = $row["product_name"];
$price = $row["price"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
if ($colindex == 1) {
$dynamicList .= "<tr>";
$rowclosed = false;
}
$dynamicList .= '<td width="142" valign="top" align="center">
<div id="products">
<div class="product">
<img style=" 1px solid;" src="inventory_images/' . $id . '.jpg" alt="' . $product_name . '" width="100" height="100" border="0" /><div class="pr-info"><h4>' . $product_name . '</h4>
<input type="checkbox" name="check[]" value="<?php .$id. ?>"/>
</div>
</div></td>';
}
}
The Code below is where i used to retrieve the data from database of selected checkbox
foreach ($_POST['check'] as $k => $check) {
$where[ ] = $k . " = '" . mysql_real_escape_string($check) . "'";
}
include "storescripts/connect_to_mysql.php";
$sql = mysql_query("Select * from products where " . implode(' AND ', $where)); // Connect to the MySQL database
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
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 .= '<td width="142" valign="top" align="center">
<div id="products">
<div class="product">
<a href="printer.php?id=' . $id . '">
<img style=" 1px solid;" src="inventory_images/' . $id . '.jpg" alt="' . $product_name . '" width="200" height="200" border="0" />
</a>
<div class="pr-info"><h4>' . $product_name . '</h4><p>' . $details . ' </p>
<span class="pr-price"><span>Rs.</span><sup>' . $price . '</sup></span>
</div>
</div></td>';
}
} else {
echo "That item does not exist.";
exit( );
}
Try changing the beginning of your second code to this:
include "storescripts/connect_to_mysql.php";
$sql = mysql_query('SELECT * FROM products WHERE id IN ('.implode(', ', array_values($_POST['check'])).')');
// rest of your code, beginning at $productCount = ...
But, how it was said before, you should start using PDO!
I think you final query and code should look this way...please change your code with this...and post the results:
foreach($_POST['check'] as $k){
$where[]= $k;}
var_dump($where); //it show you something, show me the var_dump result ???
include "storescripts/connect_to_mysql.php";
$query = "Select * from products where id IN(".implode(",",$where).")";
echo $query; //this line too....show me the result
$sql = mysql_query($query);
// Connect to the MySQL database
$productCount = mysql_num_rows($sql);
//the rest of your code ;)
-----------------------EDITED--------------------------------
Your problem is in this line:
<input type="checkbox" name="check[]" value="<?php .$id. ?>"/>
That line is wrong, make this change and see how it's work:
<input type="checkbox" name="check[]" value="<?php echo $id;?>"/>
Saludos ;)
I have the following code that shows the items added in the producs table in the database and i want to make it every 10 items creating a new array so that it won't be a long list of items.
$dynamicList = "";
$sql = mysql_query("SELECT * FROM products ORDER BY date_added DESC");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$product_name = $row["product_name"];
$price = $row["price"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
$dynamicList .= '<table width="100%" border="0" cellspacing="0" cellpadding="6">
<tr>
<td width="10%" valign="top"><img style="border:#666 1px solid;" src="inventory_images/' . $id . '.jpeg" alt="' . $product_name . '" width="93" height="102" border="1" /></td>
<td width="90%" valign="top">' . $product_name . '<br />
€' . $price . '<br />
<a class="provoli" href="product.php?id=' . $id . '">Προβολή Λεπτομεριών</a></td>
</tr>
</table>';
Your code-style could use some work, as it is not very pretty, but, to make this work, you could try putting your list in an array.
This requires two steps, the first is keeping record of an index. The second is adding the values to your new array.
$index = 0;
while(<your condition>) {
$index++;
<more code>
$list[((int)$index/10)] .= <string>
}
A better way is to implement this when printing the array with all your items. But I don't know how you're implementing this further.
I am developing a comment box for my website. When a message is posted on the website and another user comes and wants to reply, the previous message message should display below it.
I am showing the message in a <ul>. Each UL child (which is LI), contains a table that shows the message.
Link to image
Comments Showing
Suppose these are the comments showing:
##############################Comment One By David
################################################################ Reply of David
########################## Another Reply of David
########################### Comment Two By Jhon
My messages are showing using AJAX and PHP.
An AJAX script sends data, which goes to a PHP script and then after verification goes to a database and in the mean time it's also updated on the screen.
My problem is that if I want to reply to a message at the end, or want to add a new message it works fine, but when I want to reply to a message which is previously added I do not know how to put it in between html code.
Problem
Suppose I want to reply to "David", when I submit the comment it goes below the last message not between the messages.
HTML
<div id="add-comment" title="Comment">
<ol class="timeline" id="update">
<?
$retrieve = $con->select1("*", "`comments`", "page_id=" . $page_id);
$confirm_author = 0;
while ($row = mysql_fetch_array($retrieve)) {
echo'<table class="shw-comment" id="show-comment-table" ><tbody><tr>';
$name = $con->verify_author($row['name'], $row['email'], $row['website']);
echo'<td align="left" colspan="9" id="name-td">' . $name;
$niceDay = $con->perfect_date_format($row['date_and_time']);
echo'<div align="right" id="comment-date">' . $niceDay . '</div></td></tr><tr>';
echo'<td align="left" id="user-icon-td">';
if ($name == "Author") {
echo '<div class="author" id="user-icon"></div>';
} else {
$hash = md5(strtolower(trim($row['email'])));
$default_usr = urlencode('http://localhost/king-of-developers/images/user-icon.png');
echo "<div class=\"default-user\" id=\"user-icon\"><img src=\"http://www.gravatar.com/avatar/$hash.'.jpg?s=45&d=$default_usr'\" /></div>";
}
echo '</td>';
echo'<td colspan="8" id="user-comments-td" valign="top">' . $row['user_comments'] . '</td>';
echo'</tr><tr>';
echo'<td align="right" colspan="9" id="reply-td"><input type="button" class="reply" name="reply" value="reply" title="reply"/></td>';
echo'<tr>
<td align="left" colspan="9">
<input id="comment-id' . $row['id'] . '" type="hidden" value="' . $row['id'] . '"/>';
echo'</tr>';
echo'</tbody></table></li>';
if($row['respond'] == 1){
$retrieve2 = $con->select1("*", "`comment_respond`", "comment_id=" . $row['id']);
$confirm_author = 0;
while ($row2 = mysql_fetch_array($retrieve2)) {
echo'<table class="shw-comment" id="comment-reply" ><tbody><tr>';
$name = $con->verify_author($row2['name'], $row2['email'], $row2['website']);
echo'<td align="left" colspan="9" id="name-td">' . $name;
$niceDay = $con->perfect_date_format($row2['date_and_time']);
echo'<div align="right" id="comment-date">' . $niceDay . '</div></td></tr><tr>';
echo'<td align="left" id="user-icon-td">';
if ($name == "Author") {
echo '<div class="author" id="user-icon"></div>';
} else {
$hash = md5(strtolower(trim($row2['email'])));
$default_usr = urlencode('http://localhost/king-of-developers/images/user-icon.png');
echo "<div class=\"default-user\" id=\"user-icon\"><img src=\"http://www.gravatar.com/avatar/$hash.'.jpg?s=45&d=$default_usr'\" /></div>";
}
echo '</td>';
echo'<td colspan="8" id="user-comments-td" valign="top">' . $row2['user_comments'] . '</td>';
echo'</tr><tr>';
echo'<td align="right" colspan="9" id="reply-td"><input type="button" class="reply" name="reply" value="reply" title="reply"/></td>';
echo'<tr>
<td align="left" colspan="9">
<input id="comment-id' . $row['id'] . '" type="hidden" value="' . $row['id'] . '"/>';
echo'</tr>';
echo'</tbody></table></li>';
}
}
}
?>
</ol>
Ajax code
$(".submit-comment").click(function(){
var a="",b=0;
var n=$("#your-name").val();
var e=$("#your-email").val();
var w=$("#your-website").val();
var c=$("#comments").val();
var pg=$("#page-no").val();
var rp=$("#respond").val();
var ch=$("[name=recaptcha_challenge_field]").val();
var re=$("[name=recaptcha_response_field]").val();
var confirmAuthor=0;
n=$.trim(n);
e=$.trim(e);
w=$.trim(w);
c=$.trim(c);
var h="name="+n+"&email="+e+"&web="+w+"&comment="+c+"&challenge="+ch+"&response="+re+"&respond="+rp+"&page_id="+pg;
document.getElementById("recaptcha_reload_btn").click();
if(n==""||e==""||c==""||$.trim(re)==""){
a+="\n Please Write Your 'Name' , 'Email' , 'Comments' and 'Captcha' Before Submiting. ";
b++;
}else{
var i=/[-_#'$&`~;?%^)*(#!0-9]/;
var temp=n;
temp.toLowerCase();
if(temp=="author"){
a+="\nInvalid User Name";
b++;
}
if(i.test(n)){
a+="\nPlease Write a Correct Name ! ";
b++;
}
i=/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/;
if(!i.test(e)){
a+="\nPlease Write Valid Email Address ! ";
b++;
}
}
if(b>=1){
alert(a);
}
if(b==0){
$("#flash").show();
$("#flash").fadeIn(400).html('<img src="images/loading.gif" align="absmiddle"> <span class="loading">Loading Comment...</span>');
$.ajax({
type:"POST",
url:"admin/include/ajax-comments.php",
data:h,
cache:false,
success:function(a){
$("ol#update").append(a);
$("ol#update li:last").fadeIn("slow");
document.getElementById("your-email").value="";
document.getElementById("your-name").value="";
document.getElementById("your-website").value="";
document.getElementById("comments").value="";
document.getElementById("respond").value="";
$("#recaptcha_reload_btn").click();
$("#your-name").focus();
$("#flash").hide();
}
})
}
return false;
});
PHP Code
<?php
require_once('recaptchalib.php');
$privatekey = "64444444444444";
$resp = recaptcha_check_answer($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["challenge"], $_POST["response"]);
if (!$resp->is_valid) {
//What happens when the CAPTCHA was entered incorrectly
die("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
"(reCAPTCHA said: " . $resp->error . ")");
}
else
{
// Your code here to handle a successful verification
require_once '../config.php';
$con = new config();
if ($_POST) {
$name = $_POST['name'];
$email = $_POST['email'];
$web = $_POST['web'];
$comment = $_POST['comment'];
$respond = $_POST['respond'];
$page = $_POST['page_id'];
$name = $con->clean_input($name);
$email = $con->clean_input($email);
$web = $con->clean_input($web);
$comment = $con->clean_input($comment);
$name = $con->sanitizeHTML($name);
$email = $con->sanitizeHTML($email);
$web = $con->sanitizeHTML($web);
$comment = $con->sanitizeHTML($comment);
$con->validateNull($name, "Please Enter Your Name ");
$con->validateEmail($email, "Please Enter Your Valid Email");
$con->validateNull($comment, "Please Don't Leave Empty Comments");
if ($con->errorCounter == 0) {
$name = ucwords(strtolower($name));
$comment = ucfirst(strtolower($comment));
$con->setTime_zone();
$comment_time = date('Y-m-d H:i:s', time());
$comment_time = $con->perfect_date_format($comment_time);
$ip = $con->getIP();
if ($respond >= 1) {
$col[0] = "comment_id";
$col[1] = "name";
$col[2] = "email";
$col[3] = "website";
$col[4] = "user_comments";
$col[5] = "user_ip";
$col[6] = "date_and_time";
$data[0] = "'" . $respond . "'";
$data[1] = "'" . $name . "'";
$data[2] = "'" . $email . "'";
$data[3] = "'" . $web . "'";
$data[4] = "'" . $comment . "'";
$data[5] = "'" . $ip . "'";
$data[6] = "'" . $comment_time . "'";
$con->insert("`comment_respond`", $col, $data);
$con->update("`comments`", "`respond`= 1","`id`='".$respond."'");
}
if ($respond == 0) {
$col[0] = "page_id";
$col[1] = "respond";
$col[2] = "name";
$col[3] = "email";
$col[4] = "website";
$col[5] = "user_comments";
$col[6] = "user_ip";
$col[7] = "date_and_time";
$data[0] = "'" . $page . "'";
$data[1] = "'" . $respond . "'";
$data[2] = "'" . $name . "'";
$data[3] = "'" . $email . "'";
$data[4] = "'" . $web . "'";
$data[5] = "'" . $comment . "'";
$data[6] = "'" . $ip . "'";
$data[7] = "'" . $comment_time . "'";
$con->insert("`comments`", $col, $data);
$comment_id = mysql_insert_id();
}
} else {
$arraysize = count($con->errorMsg);
for ($i = 0; $i < $arraysize; $i++) {
echo $con->errorMsg[$i] . "<br>";
}
}
$confirm_author = 0;
$con->close_connection();
}
}
?>
<li>
<?php
if ($respond >= 1){
echo '<table class="shw-comment" id="comment-reply">';
}
if ($respond == 0){
echo '<table class="shw-comment" id="show-comment-table">';
}
?>
<tbody>
<?
$name = $con->verify_author($name, $email, $web);
?>
<tr>
<td align="left" colspan="9" id="name-td">
<? echo $name; ?>
<div align="right" id="comment-date">
<? echo $comment_time; ?>
</div>
</td>
</tr>
<tr>
<td align="left" id="user-icon-td">
<?
if ($name == "Author") {
echo '<div class="author" id="user-icon"></div>';
} else {
$hash = md5(strtolower(trim($email)));
$def_usr = urlencode('http://www.kingofdevelopers.com/images/user-icon.jpg');
echo "<div class='default-user' id=\"user-icon\"><img src=\"http://www.gravatar.com/avatar/$hash.'.jpg?s=45&d=$def_usr'\" /></div>";
}
?>
</td>
<td colspan="8" id="user-comments-td" valign="top">
<? echo $comment; ?>
</td>
</tr>
<tr>
<td align="right" colspan="9" id="reply-td">
<input type="button" class="reply" name="reply" value="reply" title="reply" />
</td>
</tr>
<tr>
<td align="left" colspan="9">
<?
if($respond >= 1){
$conct ='comment-id'.$respond;
echo "<input id='$conct' type='hidden' value=''/>";
}
if($respond == 0){
$conct ='comment-id'.$comment_id;
echo "<input id='$conct' type='hidden' value=''/>";
}
?>
</td>
</tr>
</tbody>
</table>
If one were to tag each comment with a unique ID, then reference this ID in your AJAX response.
Once the response is received simply find the matching ID and insert HTML comment snippet accordingly.