MySQL PHP Search using one input to search multiple columns - php

First of sorry for my English, But i'll try to make my idea clear.
My goal : use one input field to search in multiple columns. i'll give an example to make it easier and clear for u guys to understand.
I've many CPUs for different brand. so if i'm going to write "CPU" it will give me all CPUs for all brands, but if i'm going to continue writing "CPU Lenovo" it will show me the CPU only for lenovo brand. so more detail gives more specific results.
here is my code :
<?php
require ('connection.php');
if (!empty($_POST['partname'])) {
$search = explode(" ", rtrim($_POST['partname'], " "), 2);
$prlist_name = "";
$prlist_model = "";
foreach ($search AS $s) {
$prlist_name .= "`prlist_name` LIKE '%" . mysql_real_escape_string($s) . "%' AND ";
$prlist_model .= "`prlist_model` LIKE '%" . mysql_real_escape_string($s) . "%' OR ";
}
$prlist_name = substr($prlist_name, 0, -4);
$prlist_model = substr($prlist_model, 0, -4);
$query = mysql_query("SELECT * FROM `prlist` WHERE ($prlist_name) AND ($prlist_model)");
if (mysql_num_rows($query) > 0) {
while ($row = mysql_fetch_assoc($query)) {
echo "<tr>";
echo "<td>" . $row['prlist_name'] . "</td>";
echo "<td>" . $row['prlist_model'] . "</td>";
echo "</tr>";
}
}
} else {
header('Location: index.php');
exit();
}
?>
The problem with my code is searching separately. i mean that if i'm going to write "CPU Lenovo". it will show me the whole parts for Lenovo. example:
CPU | Lenovo G580
RAM | Lenovo T430
CPU | Lenovo Z500
That's it. Thanks all :)

SELECT * FROM prlist WHERE MATCH (prlist_name,prlist_model)
AGAINST ('+CPU +Lenovo' IN BOOLEAN MODE);
http://dev.mysql.com/doc/refman/5.6/en/fulltext-boolean.html

Related

Finding pattern according to variable content

I would like to do something quite simple but I dont know the right code in php.
I have a variable $go which content is GO:xxxxx
I would like to query that if the content of a variable has the pattern "GO:" and something else, echo something, but if not, echo another thing
I want to declare an if statement like:
if (preg_match('/GO/', $go) {
echo "something";
}
else {
echo "another thing";
But I cannot make it work...
I want to embed this statement between this portion of my script:
$result = mysqli_query($enlace,"select count(distinct name2) as total from " . $table . " where go_id like '%" . $go . "%'");
$items = mysqli_fetch_assoc($result);
echo "<br/>";
echo "<b> The total number of genes according to GO code is:</b> " . $items['total'];
mysqli_free_result($result);
$result2 = mysqli_query($enlace,"select count(distinct name2) as total from " . $table . " where db_object_name like '%" . $go . "%'");
$items2 = mysqli_fetch_assoc($result2);
echo "<br/>";
echo "<b>The total number of genes according to GO association is:</b> " . $items2['total'];
mysqli_free_result($result2);
As it is right now, the variable $go can have a value like GO:xxxx or a random sentence, and, with this code, I get two strings, one with value 0 and another with value according to the total apperances matching $go content.
What I want is to declare an if statement so that it just prints one string, the one that has the number of matches according to $go content, but not both.
Any help?
Use strpos():
if (strpos($go, 'GO:') !== false) {
echo 'true';
}
Try this
echo (!strpos($go, 'GO:')) ? "another thing" : "something";
Will surely work

Looping through column fields in MySQL with PHP

I'm building a food menu on a webpage. This menu includes drink items like Wine and Beer. Currently, they are categorized by Red or White and the type of wine (Pinot Grigio, Chardonnay, etc.). I have entered these wine items into a MySQL database with the following columns/values:
id(int), name(varchar), type(varchar), price_bottle(decimal), price_glass(decimal), red(bool)
My Current code:
$result = mysql_query("SELECT * FROM Wines WHERE type = 'Pinot Grigio' ");
while($row = mysql_fetch_array($result)) {
$name = $row['name'];
$type = $row['type'];
$price_bottle = $row['price_bottle'];
$price_glass = $row['price_glass'];
echo
"<div class='foodmenu-item'>
<div class='foodmenu-text'>
<h2>" . $type . "</h2>
<p>" . $name . "</p>
</div>
<div class='foodmenu-price'>
<p>$" . $price_bottle . " / " . $price_glass . "</p>
</div>
</div>";
}
Which is fine if I wanted to display each item out individually, but I want to Group by 'type' of wine (Pinot Grigio, Chardonnay, etc.) and then list each name of the wine under that 'type' so my CSS looks neat and organized.
For Example:
PINOT GRIGIO
Nobllissimo
Riff
CHARDONNAY
The Crusher
Bogle
Using the "GROUP BY" SQL syntax only displays the first result with my current code. All my attempts at making a foreach statement result in error, I'm not sure where to turn next.
How can I achieve this?
good manner is don't simply use "*", but list of columns
(reason: select only data which realy want to use - better performance, second if you later want add or change columns in the table then your life will be simplier ;-) )
I think you can try, select all from small table and only sort the data - type and name, then in while cycle do it group about type for your menu. Then you need only one query send to database and have all of it. Assumption is Wines is only small table, because select all consume a memory.
SELECT type, name, price_bottle, price_glass
FROM Wines
ORDER BY type, name;
Have you tries using 2 queries nested? Like this:
$result = mysql_query("SELECT DISTINCT(type) FROM Wines ORDER BY type");
while($row = mysql_fetch_array($result)) {
$type = $row['type'];
echo "<div class='foodmenu-item'>
<h2>" . $type . "</h2>";
$result2 = mysql_query("SELECT * FROM Wines WHERE type = '".type."' ");
while($row2 = mysql_fetch_array($result2)) {
$name = $row2['name'];
$price_bottle = $row2['price_bottle'];
$price_glass = $row2['price_glass'];
echo "
<div class='foodmenu-text'>
<p>" . $name . "</p>
</div>
<div class='foodmenu-price'>
<p>$" . $price_bottle . " / " . $price_glass . "</p>
</div>";
}
echo "</div>";
}

Fill empty cells in table

Basically I have a ranking-page that I'm working on for my CS:GO server. So far it's working fine, and it's looking like this:
https://puu.sh/aJlTj/b6fb5a7a06.png
And the next-page arrow(s) works fine, but the problem is, at page 3+, it's empty because there haven't been more players on the server yet, and it looks like this:
https://puu.sh/aJlXH/7e69071a2b.png
I am using the answer from Make multiple pages out of a mysql query in order to create the pages for all the players.
My table-printing-part looks like this:
echo "<div class='TableGen'><table border='1'><tr><td>Name</td><td>Wins</td><td>Losses</td><td>ELO</td></tr>";
$query = mysqli_query($db, "SELECT * FROM multi1v1_stats ORDER BY wins DESC LIMIT $offset,15");
while($row = mysqli_fetch_array($query)) {
echo "<tr><td>" . $row['name'] . "</td><td>" . $row['wins'] . "</td><td>" . $row['losses'] . "</td><td>" . $row['rating'] . "</td></tr>";
}
?>
</table>
$offset is declared by using $offset = 15 * intval($_GET['page']); unless it's the first page, then it's set to 0.
I understand that this code will only echo out <tr><td></td></tr>'s as long as there's data to post, so how would I go about checking if the page's cells are empty, then just echo out empty cells? (due to this, I can't use empty-cells: show; either as there are no cells to show, not even empty ones)
Thanks in advance.
I think this is what you want... It basically finds the number of rows that were returned and then continues with blanks until the limit is reached.
$limit = 15;
echo "<div class='TableGen'><table border='1'><tr><td>Name</td><td>Wins</td><td>Losses</td><td>ELO</td></tr>";
$query = mysqli_query($db, "SELECT * FROM multi1v1_stats ORDER BY wins DESC LIMIT $offset,$limit");
$i=0;
while($row = mysqli_fetch_array($query)) {
$i = $i+1;
echo "<tr><td>" . $row['name'] . "</td><td>" . $row['wins'] . "</td><td>" . $row['losses'] . "</td><td>" . $row['rating'] . "</td></tr>";
}
for($i=$i;$i<=$limit;$i++)[
echo "<tr><td> </td><td> </td><td> </td><td> </td></tr>";
}
?>
</table>

How to create a numbered list inside a table with PHP

I'm executing this type of code inside a table.
while($row = mysql_fetch_array($result))
{
echo "" . $row['id'] . "";
echo "" . $row['name'] . "";
echo "" . $row['car'] . "";
}
Which works great and gives me a numbered list (1 Mike Volvo, 2 Mike Ford) for example. The problem is that I now have multiple users in the same table so there are gaps in the list where someone else's entry is since i'm using auto incremented ID for numbers.
So assuming I have 5 entries I obviously want it to be 1-5 but right now its 5-30-45-65 or whatever.
Anyway I looked around for a solution and I found $number = 1; $number++; to be effective and it kind of works and gives me a 1-5 list independent of whatever the ID's are.
The problem is that when I reverse the list using ORDER BY xxx desc the last entry becomes 1 and the first entry becomes 5. But I always want the first entry to remain 1 and the last entry to be 5.
Do you have any ideas on how to create this function using PHP?
You need to simple user $number as you mentioned as in the following code
$number = 1;
while($row = mysql_fetch_array($result))
{
echo $number;
echo "" . $row['name'] . "";
echo "" . $row['car'] . "";
++$number;
}
Even if in your SQL you have order by it does not matter in that case.
From what I understand of your question... You are on the right track using the following code...
$LastId ='';
$number = 0;
while($row = mysql_fetch_array($result))
{
if($LastId != $row['id']){
$number++;
}
echo $number;
echo "" . $LastId = $row['id'] . "";
echo "" . $row['name'] . "";
echo "" . $row['car'] . "";
}
So...
If the 'id' isn't the same as the last 'id' (not the same user) it will increment it by 1. Otherwise it will stay the same.
Hope this is what you looking for and helps.
Although this is pretty trivial in php, I would not use php for it. You can easily do this in html:
echo "<ol>";
while($row = mysql_fetch_array($result))
{
echo "<li>" . $row['name'] . $row['car'] . "</li>";
}
echo "</ol>";

display number as image in sql

I'm designing a website for a neighbor for a potential restaurant he wants to open. I need to create a page for testimonials/review. I'd like to code it so that the number of stars is in a SQL, I'm just not sure how to do it.
Essentially what I'm looking for is a way to determine the integer of the ratings field (1-5) and for each number, display a star image.
So:
if rating = 1, display star.png once
if rating = 2, display star.png twice
...
if rating = 5, display star.png five times
I'm just not sure how to write the code to do so.
The website is being written in PHP and CSS. I'd like to avoid JQuery, Javascript, .NET, and so forth, as I'm not as familiar with them and will be the one keeping the site up to date.
Here's what I've got so far, but it's not working right, and I get a syntax error:
$result = mysqli_query($con,"SELECT * FROM Reviews");
while($row = mysqli_fetch_array($result))
{
IF (Rating = "1"()){
echo '<img src="star.png">' . }
ELSE IF (Rating = "2"()){
echo '<img src="star.png"><img src="images/star.png">' . }
Else IF (Rating = "3"()){
echo '<img src="star.png">star.png"><img src="images/star.png">' . }
ELSE IF (Rating = "4"()){
echo '<img src="star.png"><img src="images/star.png">star.png"><img src="images/star.png">' . }
ELSE
echo '<img src="star.png"><img src="images/star.png">star.png"><img src="images/star.png"><img src="images/star.png">' .
"<br/> <b>" .
$row['Name'] .
"</b> <em>" .
$row['City'] . $row['State'] . $row['Country'] . "</em><br/>" .
$row['Review'] . "<br/> <hr> <br/>"
}
?>
Use a select statement to get the ratings for a place from your database.
Store the result in a php variable (lets call it $rating)
Use php logic to output number of stars (in html obviously) based on value of $rating.
Hope that helps :)
I would recommend 3 tables for this idea.
Users Table
UserRatings Table
Dish Table
Users table is used to store just that. User information. Possibly a username, password, first name, last name for example. The table should have a primary key. Call it UsersID. It should auto increment itself and be unique for every row.
The Dish table is next. Put a dish name in it. It should have a primary key as well. Call it DishID.
Lastly is the UserRatings table will store UserRatingsId, Rating, InsertTimeStamp, UpdateTimeStamp.
Use a loop to output your HTML based on your rating.
$rating = 4; //Figure this out in your script and set accordingly
for($i = 0; $i < $rating; $i++) {
echo '<img src="star.png" />';
}
Should print out four stars for you.
Help from a friend:
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$starCount = 0;
$result = mysqli_query($con,"SELECT * FROM Reviews");
while($row = mysqli_fetch_array($result)) {
$starCount = $row['Rating'] ;
while ($starCount > 0) {
echo '<img src="images/star.png">';
$starCount -- ;
}
$starCount = 0;
echo "<br/> <b>" . $row['Name'] . "</b> - <em>" .
$row['City'] .", ". $row['State'] ." ". $row['Country'] . "</em><br/>" .
$row['Review'] . "<br/> <hr> <br/>" ;
}
?>
$number=$row->rating ;
$middle="";
$first="<td width='200' align='left'>";
for($x=1;$x<=$number;$x++) {
$middle=$middle.img($fullimage_properties);
}
if (strpos($number,'.')) {
$middle=$middle.img($halfimage_properties);
$x++;
}
while ($x<=5) {
$middle=$middle.img($blankimage_properties); ;
$x++;
}
echo $last=$first.$middle."</td>";

Categories