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%'
Related
I am new to PHP, i am now stuck at the transferring of id selected into another page.
this is my first page, home.php
<?php
require 'dbfunction.php';
$con = getDbConnect();
if (mysqli_connect_errno($con)) {
"Failed to connect to MySQL: " . mysqli_connect_error();
} else {
$result = mysqli_query($con, "SELECT * FROM category");
while ($category = mysqli_fetch_array($result)) {
?>
<div class="col-md-4">
<a class="thumbnail" href="product.php?cat=<?php echo $category['categoryid']; ?>">
<img src="<?php echo "img/" . $category['image'] ?>" width="200" height="200">
<div class="title"><h3><?php echo $category['title']; ?></h3></div>
</a>
</div>
<?php
}
mysqli_close($con);
}
?>
My second page, product.php
<?php
require 'dbfunction.php';
$con = getDbConnect();
if (mysqli_connect_errno($con)) {
"Failed to connect to MySQL: " . mysqli_connect_error();
} else {
$result = mysqli_query($con, "SELECT * FROM product");
while ($product = mysqli_fetch_array($result)) {
?>
<div class="col-md-4">
<a class="thumbnail" href="productInfo.php?cat=<?php echo $product['categoryid']; ?>&code=<?php echo $product['productname']; ?>">
<img src="<?php echo "img/" . $product['productimage']; ?>" width="250" height="220">
<div class="title"><h3><?php echo $product['productname']; ?></h3></div>
</a>
</div>
<?php
}
}
?>
data in my database,
category
categoryid PM2103
name PaperModel
image papermodel.jpg
categoryid GP4567
name GlossyPaper
image GlossyPaper.jpg
product
categoryid PM2103
productname PaperModel
Productimage papermodel.jpg
categoryid PM2103
productname PaperModel222
Productimage papermodel222.jpg
categoryid PM2103
productname PaperModel333
Productimage papermodel333.jpg
categoryid GP4567
productname GlossyPaper
Productimage GlossyPaper.jpg
categoryid GP4567
productname GlossyPaper222
Productimage GlossyPaper222.jpg
when a person select a category in home.php, it would bring the person into another page product.php, with all the product in the category list. I'm not sure what code i should use to link them together.
Your home.php page is alright ... modify product.php
<?php
require 'dbfunction.php';
$con = getDbConnect();
$cat=$_GET['cat'];
if (mysqli_connect_errno($con)) {
"Failed to connect to MySQL: " . mysqli_connect_error();
} else {
$result = mysqli_query($con, "SELECT * FROM product where categoryid='".$cat."'");
while ($product = mysqli_fetch_array($result)) {
?>
<div class="col-md-4">
<a class="thumbnail" href="productInfo.php?cat=<?php echo $product['categoryid']; ?>&code=<?php echo $product['productname']; ?>">
<img src="<?php echo "img/" . $product['productimage']; ?>" width="250" height="220">
<div class="title"><h3><?php echo $product['productname']; ?></h3></div>
</a>
</div>
<?php
}
}
?>
Change this in product.php
$cat_id = mysqli_escape_real_string($_GET['cat']);
$result = mysqli_query($con, "SELECT * FROM product WHERE categoryid={$cat_id}");
Note: But I'd recommend to use Prepared Statements/PDO to avoid SQL injection.
Read more on SQL injection: here, here
I am building a blog and trying to show all comments that apply to the post.
Each post has an ID and each comment is stored with the post ID.
here is my code:
<?php
$con = mysql_connect("localhost","cl49-XXX-b","X");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("cl49-XXX-b", $con)or die( "Unable to select database line 873");
$result=mysql_query("SELECT * FROM blogcomments WHERE ID='".$ID."'") or die('Error on Line 215 :'.mysql_error());
echo " <ul class='comments'> "; // first row beginning
for ($i = 1; $i <= mysql_num_rows($result); $i++)
{
$row = mysql_fetch_array($result);
$name = $row['name'];
$email = $row['email'];
$comment = $row['comment'];
$created=$row['created'];
echo" <li>
<div class='comment'>
<div class='thumbnail'>
<img class='avatar' alt='' src='img/avatar.jpg'>
</div>
<div class='comment-block'>
<div class='comment-arrow'></div>
<span class='comment-by'>
<strong>$name</strong>
<span class='pull-right'>
<span> <a href='#'><i class='icon-reply'></i> Reply</a></span>
</span>
</span>
<p>$comment</p>
<span class='date pull-right'>$created</span>
</div>
</div> ";
echo " </li>"; // it's time no move to next row
}
?>
When I run this code the page only shows one comment, although my DB has 3 comments with the correct ID.
I would consider using mysqli_ as mysql_ has been depreciated. I'd also consider using a while loop, hopefully this will help:
<?php
$DBServer = 'localhost';
$DBUser = 'xxxx';
$DBPass = 'xxxx';
$DBName = 'xxxx';
$mysqli = new mysqli($DBServer, $DBUser, $DBPass, $DBName);
if ($mysqli->connect_errno) {
echo "Failed to connect to the database: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
$query = "SELECT * FROM blogcomments WHERE ID='". $ID ."'";
echo " <ul class='comments'> ";
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_assoc()) {
?>
<li>
<div class='comment'>
<div class='thumbnail'>
<img class='avatar' alt='' src='img/avatar.jpg'>
</div>
<div class='comment-block'>
<div class='comment-arrow'></div>
<span class='comment-by'>
<strong><?php echo $row['name']; ?></strong>
<span class='pull-right'>
<span><a href='#'><i class='icon-reply'></i> Reply</a></span>
</span>
</span>
<p><?php echo $row['comment']; ?></p>
<span class='date pull-right'><?php echo $row['created']; ?></span>
</div>
</div>
</div>
</li>
<?php
} $result->close();
} $mysqli ->close();
echo "</ul>";
?>
I haven't tested this for bugs, but let me know if you like further information.
When you do this :
mysql_query("SELECT * FROM blogcomments WHERE ID='".$ID."'")
You select just one comment in the database.
Maybe you have to do that :
mysql_query("SELECT * FROM blogcomments WHERE post_ID='".$post_ID."'")
Because you are selecting the row where ID field value is $ID in the table blogcomments.
I guess you store the referred post_id to whom the comment is connected in a field called something like "post_id". You better point your select query to that field ;)
If you put the structure of the table in the question, I might help :)
You're not closing your <ul>. Might it just be a HTML formatting issue?
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 tried the empty selector in jquery but it doesnt work. the content is still being displayed. i am retrieving some rows from the SQL database.. if there is no database then i dont want to display that div.
<div id="scrollingText">
<div class="scrollWrapper">
<div class="scrollableArea">
<marquee behavior="scroll" direction="left">
<p>
<?php
$con = mysql_connect("localhost","fraptech_test","");
mysql_select_db("fraptech_test", $con);
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysql_select_db("fraptech_ndsnotice", $con);
$result = mysql_query("SELECT * FROM ndsnotice");
while($row = mysql_fetch_array($result))
{
echo $row['Notice'];
}
?>
</p>
</marquee>
</div>
</div>
</div>
Is that your whole code? Are you working with ajax? If you're using pure PHP without ajax just set the output div into your "if"-conditions?
If you want to use jQuery, try:
if ( ($("div.scrollableArea p").text()).length > 0 )
{
$("div.scrollableArea p").show();
}
else
{
$("div.scrollableArea p").hide();
}
But you can do it without jQuery, I guess.
You need to put the HTML inside your php if clause.
Simple example:
<div id="scrollingText">
<?php
$con = mysql_connect("localhost","fraptech_test","");
mysql_select_db("fraptech_ndsnotice", $con);
$result = mysql_query("SELECT * FROM ndsnotice");
if (mysqli_connect_errno($con)) {
<div class="scrollWrapper">
<div class="scrollableArea">
<marquee behavior="scroll" direction="left">
<p>
<?php
while($row = mysql_fetch_array($result))
{
echo $row['Notice'];
}
?>
</p>
</marquee>
</div>
</div>
<?php } else { ?>
<div class="errordiv">Display this on error</div>
<?php } ?>
</div>
thank you guys with ur help i made some change. the below code did it:
<?php
$con = mysql_connect("localhost","fraptech_test","");
mysql_select_db("fraptech_test", $con);
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysql_select_db("fraptech_ndsnotice", $con);
$result = mysql_query("SELECT * FROM ndsnotice");
if (mysql_num_rows($result) > 0)
{
?><div id="scrollingText">
<div class="scrollWrapper">
<div class="scrollableArea">
<marquee behavior="scroll" direction="left">
<p>
<?php while($row = mysql_fetch_array($result))
{
echo $row['Notice'];
}
?> </p>
</marquee>
</div>
</div>
</div>
<?php } ?>
I am trying to get the code below to loop using a while statement. The problem I am having is getting the divs to worth with in the PHP. Any ideas on how I can make this happen? My database consists of a columns called month, day, description, and location.
<div class="events-module">
<?php
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("sandbox", $con);
$result = mysql_query("SELECT * FROM events"); ?>
<?php while($row = mysql_fetch_array($result)) ?>
<div class="date">
<div id="month"><h4><?php echo $row['month']; ?></h4></div>
<div id="day"><h3><?php echo $row['day']; ?></h3></div>
</div>
<div class="event">
<div id="description"><p><?php echo $row['description']; ?></p></div>
<div id="location"><p><b><?php echo $row['location']; ?></b></p></div>
</div>
<?php endwhile;
mysql_close($con);
?>
</div>
You didn't say exactly what's wrong. But, it seems you are missing your start block for your while loop
while (.... ) :
^^^
... // div stuff
end while