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> ";
}
}
?>
Related
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.
The search results should display like this
But my results are stacking on top of each.
Here is my code :
<div class="container">
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("thesis") or die(mysql_error());
$search = trim( $_POST['SearchKeywords']);
$query = " SELECT * FROM new_data WHERE Product_Title or Product_link LIKE'%$search%' ";
$sql = mysql_query($query) or die(mysql_error());
$count = mysql_num_rows($sql);
$count == 0;
if ($count == 0) {
echo "Sorry, Nothing Found !!";
}else{
while ($row = mysql_fetch_array($sql))
{
$img = $row ['Product_Img'];
$link = $row ['Product_link'];
$title = $row ['Product_Title'];
$price = $row ['Product_Price'];
?>
<div class="card">
<div class="front alert alert-success">
<?php echo "$title";
echo "<img src='$img' width='80px' height='100px'>";
echo "$price"; ?>
</div>
</div>
<?php
};
};
?>
</div> <!-- Container -->
Those div blocks are inside a container.
I added a bootstrap class in order for better a design.
You can use thumbnails with custom content
<div class="row">
<div class="col-sm-6 col-md-4">
<div class="thumbnail">
<img src="..." alt="...">
<div class="caption">
<h3>Thumbnail label</h3>
<p>...</p>
<p>Button Button</p>
</div>
</div>
</div>
</div>
I used a counter inside while loop.
Which will check, when there are already 4 blocks/ products in a single row then it will create a new row
<?php
if($productCount == 0)
{
echo "<div class='row'>"; }
}
$productCount++;
if($productCount==4)
{
echo "</div>" ;
$productCount = 0;
}
?>
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
}
My php slider is sort of working i have managed to get it to link to my database. However, i need it to loop through all the images within my slider and my code isn't work but i believe i have missed a loop query?!
THIS IS MY CODE WHERE MY SLIDER IS:
<div class="theme-dark 16 columns">
<div id="slider" class="nivoSlider">
<img src="<?php print $row['image']?>"/>
<!-- <img src="images/nivo/arts.png" alt="the grand theatre and nothern ballet">
<img src="images/nivo/slider3.png" alt="leeds night light slider image">
<img src="images/nivo/slider2.png" alt="Leeds Trinity slider image">
<img src="images/nivo/slider4.png" alt="leeds art hotels">
<img src="images/nivo/slider1.png" alt="leeds art slider image"> -->
</div>
</div>
THIS IS MY CODE TO RUN MY SLIDER AT THE MOMENT:
<?php
$myQuery = "SELECT * FROM SliderImg";
$result = $con->query($myQuery);
if (!$result) die('Query error: ' . mysqli_error($con));
$row = mysqli_fetch_array($result);
?>
you need a loop in your php:
<?php
$myQuery = "SELECT * FROM SliderImg";
$result = $con->query($myQuery);
if (!$result) die('Query error: ' . mysqli_error($con));
$rows = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$rows[] = array(
'image' => $row['image'];
)
}
?>
And in your HTML:
<div class="theme-dark 16 columns">
<div id="slider" class="nivoSlider">
<?php
foreach($rows as $row) { ?>
<img src="<?php print $row['image']?>"/>
<?php
} ?>
</div>
</div>
<?php
$myQuery = "SELECT * FROM SliderImg";
$result = $con->query($myQuery);
if (!$result) die('Query error: ' . mysqli_error($con));
?>
<div class="theme-dark 16 columns">
<div id="slider" class="nivoSlider">
// use a while here
<?php while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){ ?>
<img src="<?php print $row['image']?>"/>
<?php } ?>
</div>
</div>
you can use foreach to loop through your data, like this
<div class="theme-dark 16 columns">
<div id="slider" class="nivoSlider">
<?php foreach ($row as $key => $item): ?>
<img src="<?php echo $item['image']?>"/>
<?php endforeach; ?>
</div>
</div>
hope it help..
I have coded a search for my page, however i am wanting this to showproducts that contain the search criteria... At the moment it only shows results that begin with the cirteria.
Here is my code:
<?php
$name = $_GET["q"];
if ($name <= "") {
echo ("");
}
else {
$con = mysql_connect("localhost", "cl49-XXX", "vogalcms");
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() . ')');
mysql_select_db("cl49-vogalcms", $con) or die("Unable to select database");
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='admin/userpics/$prodID.jpg' height='50' width='50' 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/Edit</span></a>
</div>
</div> ";
}
}
?>
I would like to be able to search ca and not just show CAr, CArpet but also show nesCAfe?
just change your where clause to this:
WHERE `prodname` LIKE '%$name%'
Change your search query to
SELECT * FROM products WHERE `prodname` LIKE '%$name%'