I have a problem with my comment system. At the moment it is just simple adding to database sorting and showing under right post on the blog. I am creating, I will implement ajax etc later.
The issue I am having right now is that it shows the last comment on the post below the one I added comment on. So lets say I am adding comment on post 10, it will show comment on post 10 and 11. It sorts correctly, but it duplicates to the post below too. (It doesn't duplicate in database just the way it displays.)
<?php
// Connect to the database
include('../acp/db/db.php');
$link = mysql_connect($dbhost, $dbuser, $dbpassword, $dbname);
mysql_select_db($dbname);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$result = mysql_query('SELECT * FROM `posts` ORDER BY id DESC') or die(mysql_error());
while($row = mysql_fetch_array($result)) {
$id_post = $row['id'];
echo " <!-- Blog Post Content Column -->
<h1> " . $row['post_title'] . " </h1><p class='lead'>
by <a href='#'>Matt</a></p> <hr>
<p><span class='glyphicon glyphicon-time'>" . $row['date_created'] . "</span></p>
<img class='img-responsive' style='width: 900px;height: 300px;' src=" . $row['post_img'] . "> <hr>
<p class='lead'>" . $row['post_first'] . "</p>
<p>" . $row['post_second'] . "</p> <hr>
<!-- Comments Form -->
<div class='well'>
<h4>Leave a Comment:</h4>
<form id='form'method='POST'action='php/insert-comment.php'>
<input type='hidden' name='post_id' value='$id_post'>
<input type='text' id='comment-name' name='name' placeholder='Your name' />
<input type='text' id='comment-mail' name='mail' placeholder='Your e-mail adress' />
<textarea type='text' name='comment' class='the-new-com' rows='3'></textarea>
<input type='submit' id='submit' class='bt-add-com' value='Submit Comment'></input>
</form>
</div>
<hr>
<div class='media comment-block'>
<a class='pull-left' href='#'>
<img class='media-object' src=' $grav_url' >
</a>
<div class='media-body'>$name
<h4 class='media-heading'>
<small>$date</small>
</h4>
$comment
</div>
</div>";
$resultcomments = mysql_query("SELECT * FROM `comment` WHERE post_id = '$id_post'") or die(mysql_error());
while($affcom = mysql_fetch_assoc($resultcomments)){
$name = $affcom['name'];
$email = $affcom['mail'];
$comment = $affcom['comment'];
$date = $affcom['date'];
$default = "mm";
$size = 35;
$grav_url = "http://www.gravatar.com/avatar/".md5(strtolower(trim($email)))."?d=".$default."&s=".$size;
echo"
<!-- Posted Comments -->
<!-- Comment -->
<div class='media comment-block'>
<a class='pull-left' href='#'>
<img class='media-object' src=' $grav_url' >
</a>
<div class='media-body'>$name
<h4 class='media-heading'>
<small>$date</small>
</h4>
$comment
</div>
</div>";
}
}
?>
I am slowly learning PHP so be easy on me. I feel like the issue is easy to fix, and it is right there I just cannot work out what it is.
Related
I have a working code that can insert and update information from the database and echoing it to page. but I like to hide the specific empty column while displaying all the information from the row. check this screenshot
my goal is to hide the "image" column when its null/empty; so the crock image wont display. here is my code below:
<?php
$result = mysqli_query($mysqli, "SELECT * FROM blogs ORDER BY id DESC");
while ($row = mysqli_fetch_array($result))
if (!empty($row['image'] != ""))
{
echo "<div class='row'>
<div class='col-lg-12 box'>
<div class='content-heading'>
<p>
<text>".$row['title']."</text>
</p>
</div>
<p>
<text1 class='pull-right' >".$row['image_text']."</text1><br/>
<img class='img-size' id='hp'src='admin/upload_images/".$row['image']."'/>
<text>".$row['definition']."</text>
</p>
</div>
</div>";
}
?>
but this code if (!empty($row['image'] != "")) is hiding the entire row from my database.
Can anyone have a right solution to my problem?
You could try using the style attribute of the <img> tag to selectively show or hide the image tag:
<img class='img-size'
id='hp'
style='display: '. ($row["image"] != "" ? "block" : "none") . ';'
src='admin/upload_images/".$row['image']."'/>
Instead of hiding entire row, you could just hide the image tag, like this:
<?php
$result = mysqli_query($mysqli, "SELECT * FROM blogs ORDER BY id DESC");
while ($row = mysqli_fetch_array($result))
{
echo "
<div class='row'>
<div class='col-lg-12 box'>
<div class='content-heading'>
<p>
<text>".$row['title']."</text>
</p>
</div>
<p>
<text1 class='pull-right' >".$row['image_text']."</text1><br/>
";
if (!empty($row['image'])) {
echo "<img class='img-size' id='hp'src='admin/upload_images/".$row['image']."'/>";
}
echo "
<text>".$row['definition']."</text>
</p>
</div>
</div>
";
}
?>
Your if condition will hide all other html elements as well instead you should hide only img tag when no value found for image.
Update your code as below :
while ($row = mysqli_fetch_array($result)) {
$image = (!empty($row['image'])) ? "<img class='img-size' id='hp'src='admin/upload_images/" . $row['image'] . "'/>" : "";
echo "<div class='row'>
<div class='col-lg-12 box'>
<div class='content-heading'>
<p>
<text>" . $row['title'] . "</text>
</p>
</div>
<p>
<text1 class='pull-right' >" . $row['image_text'] . "</text1><br/>
" . $image . "
<text>" . $row['definition'] . "</text>
</p>
</div>
</div>";
}
I'm currently working on a super simple Online shop idea with a shopping cart. At the end i want to print out a table with the products you ordered. Currently using a foreach but i have no idea how to solve this. I tried to use sessions as a way to give the loop an idea how many different products are ordered. But it seems like the $_SESSION way will take all the current active sessions. And using a simple variable f.e. $piano will make it print 2 rows ( using 2 piano's in my shop, tried to solve it with a if (session active) $piano1 = active. But it seems the foreach statement doesn't give a whoop about that and will print 2 rows anyways.
Sorry for the long block of text. Here's my page. Again apologies. I just started php.
Variable names are dutch but that shouldn't really matter for you guys i think.
Starting from line 103.
Thanks in advance!
<div class="logincontainer"><!-- Php Session Script Actief? -->
<?php
session_start();
if(isset($_SESSION['naam'])) :
echo "<div class='content_login'>";
echo "Hallo " . $_SESSION['naam'] . ". Welkom bij de Pianoshop.</br></br>";?>
<form method='post' action='uitlog.php'>
<input type='submit' name='loguit' Value='Loguit!'></form><br />
<form action='winkelmand.php' class="left">
<input type='image' src='images/winkelwagen-knop.png'/>
</form><br />
<form method='post' name='emptycart' action='emptycart.php' class="right">
<input type="submit" id="submitpic" name="leegwinkelmand" value="">
<?php
if(isset($_SESSION['winkelmand'])) {
echo $_SESSION['aantalproducten'] . " Item(s) - €" . $_SESSION['totaalprijs'] . ",-";
} else {
echo "Jouw winkelwagen is leeg.";
}?>
</form>
</div>
<?php else :?>
<div class='content_login'>
<form method='post' action='checklogin.php'>
<p><input type='text' name='gebruikersnaam' required='required' value='' placeholder='Gebruikersnaam'></p>
<p><input type='password' name='password' required='required' value='' placeholder='Wachtwoord'></p>
<font color="red"><p class='submit'>
<input type='submit' name='login' value='Login'>
<?php if(isset($_SESSION['logged_in'])) :?>
Verkeerd wachtwoord.
<?php session_destroy();
endif; ?>
</p></font>
<p>Nog niet geregistreerd? Doe dat hier!.</p>
</form>
</div>
<?php endif; ?></div>
<div id="site">
<div id="menubar">
<div id="logo">
<img src="images/pianotoetsen.png" >
</div>
<div id="menu_items">
<ul id="menu">
<li>Home</li>
<li>Toetsinstrumenten</li>
<li>Jouw account</li>
<li class="current">Winkelmand</li>
<li>Contact</li>
</ul>
</div></div>
<div id="site_content">
<div class="sidebar_container">
<div class="sidebar">
<h2>Sale!</h2>
<div id="thumbnail"><img src="images/piano1.jpg"></div>
<p>Yamaha CLP-575 voor maar €2599,- !</p>
<div id="thumbnail"><img src="images/piano2.jpg"></div>
<p>Ritmuller 120SL €4999,- !</p>
</div>
<div class="sidebar">
<h2>Laatste Updates</h2>
<h3>Juni 2015</h3>
<p>Site in constructie.</p>
</div>
<div class="sidebar">
<h3>Wij zijn op Facebook</h3>
<p>Klik hier.</p>
</div>
</div>
<div id="content">
<div id="wallpaperbanner">
<img src="images/banner.jpg">
</div>
<div class="content_item">
<h1>Winkelmand</h1>
<?php
$user = 'root';
$pass = '';
$db = 'online shop';
$conn = mysql_connect('localhost', $user, $pass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
if(isset($_SESSION['winkelmand'])) {
echo "Deze producten staan in je winkelwagen</br></br>";
if(isset($_SESSION['totaalprijs2']) == 0) {
// Do nothing?
} else {
mysql_select_db($db);
$query = mysql_query("SELECT * FROM product WHERE productnummer='2'");
$productgegevens = mysql_fetch_row($query);
$piano["piano2"] = "ritmuller";
$pianoarray[1] = $productgegevens['1'];
$pianoarray[2] = $productgegevens['2'];
$pianoarray[3] = $productgegevens['4'];
$pianoarray[5] = $productgegevens['3'];
$pianoarray[4] = $_SESSION['aantal_prod2'];
}
if(isset($_SESSION['totaalprijs1']))
{
mysql_select_db($db);
$query = mysql_query("SELECT * FROM product WHERE productnummer='1'");
$productgegevens = mysql_fetch_row($query);
$piano["piano1"] = "yamaha";
$pianoarray[4] = $_SESSION['aantal_prod1'];
$pianoarray[1] = $productgegevens['1'];
$pianoarray[2] = $productgegevens['2'];
$pianoarray[3] = $productgegevens['4'];
$pianoarray[5] = $productgegevens['3'];
$pianoarray[4] = $_SESSION['aantal_prod2'];
}
echo "<br />
<table width='80%' >
<thead>
<tr><th>Productnaam</th><th>Merk</th>
<th>Voorraad</th><th>Aantal</th><th>Prijs</th>
</tr>
</thead>
<tbody>";
foreach($piano as $key => $value) {
echo $key . "</br>" . $value . "<br />";
$row = "<tr>";
for ($x=1; $x<=sizeof($pianoarray); $x++){
$row = $row . "<td>" . $pianoarray[$x] . "</td>";
}
$row = $row . "</tr>";
echo $row;
}
echo "<tr><td></td><td></td><td></td><td></td><td>" . '€' . $_SESSION['totaalprijs'] . ',-' . "</td></tr></tbody></table>";
}
else {
echo "Jouw winkelwagen is leeg. <br />" . "Klik <a href='toetsinstrumenten.php'>Hier</a> om wat items toe te voegen.";
}?>
</div>
</div>
</div>
</div>
Create an array variable in the $_SESSION array and do a foreach loop on that
$_SESSION['cart']['piano1'] = 'piano1';
$_SESSION['cart']['piano2'] = 'piano2';
$cart = $_SESSION['cart'];
foreach ($cart as $key => $item) {
//do something with $item or $key
}
Hi guys I have an application that allows users to post content on an empty div. I need some advice on making it scalable as I have to make sure if users are posting a lot of content the browser will not slow down or crash. I am a beginner so please put up with me. So in my code below I have an echo statement that prints out html content. I want to know how to make that function scalable in the long run? should I set the html as a variable then print it? should I encode it with JSON? please advice me as such. Thank-you
PHP Code
<?php
include_once("connection.php");
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
//You can use Inner Join to display the matched records in between the two tables
$query = "SELECT * FROM `qpost` A INNER JOIN `user` B ON A.id=B.id ";
$result = $conn->query($query);
if($result) {
} else {
echo "bitch: " . $conn->error;
}
while($row = $result->fetch_assoc()) {
$time = time();
if( $time < $row['logged_time'] + 30) {
echo " <div class='row'>
<div class='col-md-8'>
<div id='thePost' class='panel panel-default'>
<div class='panel-heading'>
<h3 class='panel-title'><span class='glyphicon glyphicon-user'> <b>{$row['username']}</b></span>       <span class='glyphicon glyphicon-time'></span> Posted_on: {$row['time']} </h3>
</div>
<div class='panel-body' style='word-break:break-all'>
<h4>{$row['question']} </h4>
<p> {$row['description']}</p>
</div>
<div class='panel-footer'>
<button class='btn btn-primary'>Request Connection</button>
<button class='btn btn-success'>Chat <span class='glyphicon glyphicon-comment'></button>
</div>
</div>
</div>
</div> ";
}//end if
}//end while
?>
Did you try to separate the data logic and data display ? if you used php MVC framework which will more convenient on your case.
Following is a simple example.
//demo.html code
<?php
include_once("connection.php");
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
//You can use Inner Join to display the matched records in between the two tables
$query = "SELECT * FROM `qpost` A INNER JOIN `user` B ON A.id=B.id ";
$result = $conn->query($query);
if ($result) {
} else {
echo "bitch: " . $conn->error;
}
$time = time();
$data_result = array(); // focus on your data logic process
while ($row = $result->fetch_assoc()) {
if ($time < $row['logged_time'] + 30) {
$data_result[] = $row;
}
}
?>
<?php foreach($data_result as $index => $row) { ?>
<div class='row'>
<div class='col-md-8'>
<div id='thePost' class='panel panel-default'>
<div class='panel-heading'>
<h3 class='panel-title'><span class='glyphicon glyphicon-user'> <b><?php echo $row['username']?></b></span>
      <span class='glyphicon glyphicon-time'></span> Posted_on: <?php echo $row['time']?>
</h3>
</div>
<div class='panel-body' style='word-break:break-all'>
<h4><?php echo $row['question']?></h4>
<p> <?php echo $row['description']?></p>
</div>
<div class='panel-footer'>
<button class='btn btn-primary'>Request Connection</button>
<button class='btn btn-success'>Chat <span class='glyphicon glyphicon-comment'></button>
</div>
</div>
</div>
</div>
<?php } ?>
It's fine what you're doing.
However as Eric has said you can exit and re-enter your PHP
You could store the html in a file, grab it using file_get_contents then use sprintf to manipulate it
In a file called "row.html"
<div class='row'>
<div class='col-md-8'>
<div id='thePost' class='panel panel-default'>
<div class='panel-heading'>
<h3 class='panel-title'>
<span class='glyphicon glyphicon-user'>
<b>%s</b> <!-- username -->
</span>      
<span class='glyphicon glyphicon-time'>
Posted_on: %s <!-- time -->
</span>
</h3>
</div>
<div class='panel-body' style='word-break:break-all'>
<h4>%s</h4> <!-- question -->
<p> %s</p> <!-- description -->
</div>
<div class='panel-footer'>
<button class='btn btn-primary'>Request Connection</button>
<button class='btn btn-success'>Chat <span class='glyphicon glyphicon-comment'></button>
</div>
</div>
</div>
</div>
Then in your PHP....
<?php
while($row = $result->fetch_assoc()) {
$time = time();
if( $time < $row['logged_time'] + 30) {
$rowHtml = file_get_contents('row.html');
echo sprintf($rowHtml,
$row['username'],
$row['time'],
$row['question'],
$row['description']);
}
}
?>
I personally like as much abstraction as possible between my languages :)
Good luck!
One way to echo out HTML with PHP is using Heredoc syntax (http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc)
<?php
$name = 'MyName';
echo <<<MYHTML
<p>
My name is <b>"$name"</b>.
</p>
MYHTML;
?>
My coding id for search results.
I am showing results from a search, at the moment it will only show the first result, how can i make this show all results that match the search?
Here is my code
<?php
$name=$_GET["q"];
if ($name<=""){echo( "");
}
else
{
$con = mysql_connect("localhost","cl49-XXX","XXX");
if (!$con)
{
die('Could not connect: line 513 ' . mysql_error());
}
mysql_select_db("cl49-xxx", $con)or die( "Unable to select database");
$result=mysql_query("SELECT * FROM products WHERE `prodname` LIKE '$name%' ")or die('Error: Line 519 ('.mysql_error().')' );
$row = mysql_fetch_array($result);
$prodID=$row['prodID'];
$prodname=$row['prodname'];
$catagory=$row['catagory'];
}
echo"
<div class='row-fluid portfolio-block'>
<div class='span5 portfolio-text'>
<img src='userpics/$prodID.jpg' height='80' width='80' alt='' />
<div class='portfolio-text-info'>
<h4>$prodname</h4>
<p></p>
</div>
</div>
<div class='span5'>
<div class='portfolio-info'>
Product ID
<span>$prodID</span>
</div>
<div class='portfolio-info'>
catagory
<span>$catagory</span>
</div>
</div>
<div class='span2 portfolio-btn'>
<a href='edit_product.php?q=$prodID' class='btn bigicn-only'><span>View</span></a>
</div>
</div> ";
?>
Any help would be great!
Use while loop like this: while ($row = mysql_fetch_array($result)) { }
<?php
$name = $_GET["q"];
if ($name <= "") {
echo ("");
}
else {
$con = mysql_connect("localhost", "cl49-XXX", "XXX");
if (!$con) {
die('Could not connect: line 513 ' . mysql_error());
}
mysql_select_db("cl49-xxx", $con) or die("Unable to select database");
$result = mysql_query("SELECT * FROM products WHERE `prodname` LIKE '$name%' ") or die('Error: Line 519 (' . mysql_error() . ')');
while ($row = mysql_fetch_array($result)) {
$prodID = $row['prodID'];
$prodname = $row['prodname'];
$catagory = $row['catagory'];
echo "
<div class='row-fluid portfolio-block'>
<div class='span5 portfolio-text'>
<img src='userpics/$prodID.jpg' height='80' width='80' alt='' />
<div class='portfolio-text-info'>
<h4>$prodname</h4>
<p></p>
</div>
</div>
<div class='span5'>
<div class='portfolio-info'>
Product ID
<span>$prodID</span>
</div>
<div class='portfolio-info'>
catagory
<span>$catagory</span>
</div>
</div>
<div class='span2 portfolio-btn'>
<a href='edit_product.php?q=$prodID' class='btn bigicn-only'><span>View</span></a>
</div>
</div> ";
}
}
?>
you have to loop through you results like this:
while($row = mysel_fetch_array($result)){
$prodID = $row['prodID'];
$prodname = $row['prodname'];
$catagory = $row['catagory'];
echo "
<div class='row-fluid portfolio-block'>
<div class='span5 portfolio-text'>
<img src='userpics/$prodID.jpg' height='80' width='80' alt='' />
<div class='portfolio-text-info'>
<h4>$prodname</h4>
<p></p>
</div>
</div>
<div class='span5'>
<div class='portfolio-info'>
Product ID
<span>$prodID</span>
</div>
<div class='portfolio-info'>
catagory
<span>$catagory</span>
</div>
</div>
<div class='span2 portfolio-btn'>
<a href='edit_product.php?q=$prodID' class='btn bigicn-only'><span>View</span></a>
</div>
</div> ";
}
Still this is quite messy, and even worse, deprecated. Try to switch to mysqli_* functions or PDO. And think about capsulating code in classes or at least functions to clean up things a little bit.
first you need to find how many lines of code are there in $result...$numberofrows = mysql_num_rows($result) and then make cycle, for example for ($i =0; $i<$numberofrows; $i++) and then you need to echo all the rows you get in the cycle
by the way...using function mysql_* is deprecated, so you rather use PDO or mysqli...
Do your $row assignment in a while block, something like this
<?php
$name=$_GET["q"];
if ($name<=""){echo( "");
}else {
$con = mysql_connect("localhost","cl49-XXX","XXX");
if (!$con) {
die('Could not connect: line 513 ' . mysql_error());
}
mysql_select_db("cl49-xxx", $con)or die( "Unable to select database");
$result=mysql_query("SELECT * FROM products WHERE `prodname` LIKE '$name%' ")or die('Error: Line 519 ('.mysql_error().')' );
while($row = mysql_fetch_array($result)) {
$prodID=$row['prodID'];
$prodname=$row['prodname'];
$catagory=$row['catagory'];
echo"
<div class='row-fluid portfolio-block'>
<div class='span5 portfolio-text'>
<img src='userpics/$prodID.jpg' height='80' width='80' alt='' />
<div class='portfolio-text-info'>
<h4>$prodname</h4>
<p></p>
</div>
</div>
<div class='span5'>
<div class='portfolio-info'>
Product ID
<span>$prodID</span>
</div>
<div class='portfolio-info'>
catagory
<span>$catagory</span>
</div>
</div>
<div class='span2 portfolio-btn'>
<a href='edit_product.php?q=$prodID' class='btn bigicn-only'><span>View</span></a>
</div>
</div> ";
}
}
?>
I have searched a lot and am still having some trouble passing a Unique Id to a Value and then using that ID for a PHP MySQL Query to get the book info.
I currently am passing the unique ID to the Modal and Displaying it through an input type text. I am just having a hard time figuring out how to put the id into a php Variable
Here is my HTMl/PHP code
echo '<a data-toggle="modal" data-id="' . $book_id .'" title="Add this item" class="open-AddBookDialog btn btn-primary" href="#addBookDialog">View Info</a>';?>
<!-- Modal -->
<div class="modal hide" id="addBookDialog">
<div class="modal-header">
<button class="close" data-dismiss="modal">×</button>
<h3>Book Info</h3>
</div>
<div class="modal-body">
<p>some content</p>
<?php
echo $test = '<input type="text" name="bookId" id="bookId"/>';
$book_info = get_book_info($book_id);
while($row1 = mysqli_fetch_array($book_info))
{
$email = $row['email'];
echo "<p><strong>Title: </strong>" . $row1['title'] . "</p>";
echo "<p><strong>Author: </strong>" . $row1['author'] . "</p>";
echo "<p><strong>Edition: </strong>" . $row1['edition'] . "</p>";
echo "<p><strong>ISBN: </strong>" . $row1['isbn'] . "</p>";
echo "<p><strong>Price: </strong>" . $row1['price'] . "</p>";
echo "<p><strong>Seller's Name: </strong>" . $row1['name'] . "</p>";
echo "<p><strong>Seller's E-mail: </strong><a href='mailto:$email'>$email</a></p>";
}?>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
</div>
</div>
here is my jquery
$(document).on("click", ".open-AddBookDialog", function () {
var myBookId = $(this).data('id');
$(".modal-body #bookId").val( myBookId );
});
You need to echo the value:
data-id="<?php $book_id ?>"
Use this instead:
data-id="<?php echo $book_id; ?>"
You could do something like this by javascript/coffee script.
($ "a[data-toggle=modal]").click ->
target = ($ #).attr('data-target')
url = ($ #).attr('href')
($ target).load(url)
URL = unique ID