PHP search different fields in a table and show respective results - php

Maybe I'm not thinking this through correctly but I have a company table with:
name
address
city
state
zip
I want to search two fields, name and city.
My query is:
SELECT DISTINCT city, name
FROM companies
WHERE city
LIKE '%$search%'
OR name
LIKE '%$search%'
and the results code is:
echo '<a href="city.php?city=' . $results['city'] . '">'
. $results['city'] . '</a>';
Now this only takes into account if the person searches for a city. How do I get it to show the page if someone searches for a company name? Something like:
echo '<a href="company.php?co=' . $results['name'] . '">'
. $results['name'] . '</a>';
Is there any way to show the proper result depending on what they searched for?

If you have only one search box, how can you know what the user is looking for?
You could add checkboxes/radio buttons to offer the choice between city or name.
If you don't want to do that then you do not know what the user is searching for. You can however run two queries, one for city then another one for name. If you get results you will at least know where they are coming from.

Why don't you compare the two fields of the results to the search text, to know what matched it: city or name.
Something like:
if($search === $results['city']) echo ''. $results['city'] . ''; elseif($search === $results['name'])
echo ''. $results['name'] . '';
Not sure if I understood right...

Related

Trying to load/execute two table values at the same time

I'm trying to create a page that displays content from database when specific title is selected, and all the comments added to it (only comments for the selected title should be visible) by other users. I want make the id and review_id to work together on the same line. I cant create two separate echo's as it creates two separate titles.
I've tried to link the values together but cant make it work.
echo '<li>' . $comment['name'] . ' - ' . $comment['date'] . '</li>';
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
echo '<li>' . $comment['name'] . ' - ' . $comment['date'] . '</li>';
Is there a way to make "id" and "review_id" work at the same line ?

avoid reloading page or resetting select dropdowns with php

I'm new to PHP programming and working with backend. I know what I am doing, but having trouble finding a proper solution online which matches my case.
Basically, I have a form (2 text fields, 3 dropdowns) that filters the data that I am getting from SQL Server (that works). However, when I press the "filter" button and I get the desired results, the page reloads and resets the form. Now I have the filtered records but I don't know what filters I used to get that. I could insert a <?php ... ?> if else, but the thing is I already am using php to get the dropdown values. Don't think I can put php inside php.
By including if-else I mean this:
echo '<option value="' . $row['status'] . '"' . if($prod_status == $row['status']): echo ' selected="selected"' . '>' . $row['status'] . '</option>';
Here's the code:
<label for="prod_status_filter">Prod Status</label>
<select class="form-control" id="prod_status_filter" name="prod_status_filter">
<?php
$query_status = 'select DISTINCT status FROM analytics.laborrecords ORDER BY status ASC';
$result = sqlsrv_query($conn, $query_status);
while ($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC))
{
echo '<option value="' . $row['status'] . '">' . $row['status'] . '</option>';
}
?>
</select>
This is one of the dropdowns.
I want:
1. Either stop the page from reloading so the filter dropdowns remain the same. (AJAX won't work, I have 3 dropdowns with a lot of options and it's doesn't seem like a good practice to make so many getElementById lines)
2. Let the page reload but keep the values as I set until I want to reset them, with a "reset" button.
You have to populate the form using the submitted form data. It should be either in $_POST or $_GET.
Check if the value of $_POST['prod_status_filter'] matches the value in $row['status'] and if it does, echo selected.
Be sure to sanitize the form data. Never trust user input.

How to explode GROUP_CONCAT and add items inside links

I want to make some selection and read from mysql database in php.
I create this query:
$sql0="SELECT descrizioni_marche.Id_marca, descrizioni_marche.Id_categoria,
marche.ID as id_man,marche.marca as maname ,GROUP_CONCAT(categorie.categoria) as cat,categorie.ID as id_cat
FROM marche,descrizioni_marche,categorie
WHERE descrizioni_marche.Id_marca=marche.ID
AND descrizioni_marche.Id_categoria=categorie.ID
GROUP BY maname
";
I want to output some of the categories named "cat" like below:
<b>' . $row0["cat"] ."". '</b>
But because I have used GROUP_CONCAT all the $row0["cat"] display in one row separated by comma and included in one link. I want them to be in different rows and each one of them to have a different link. Is there a way to do this?
Thanks!
After I do this according to a comment below:
echo ' <b>' . explode(",", $row0['cat']) ."". '</b>';
The result is:Array to string conversion in
To print all link at same place
array_walk((explode(',',$row0['cat'])),function($category){
echo '' . $category .'';
});

Displaying values from multiple MySql database fields together as one

I'm doing a project where I have to take in information from a user on a web page and store it in a MySql database. Some of the fields the user has to fill out are his street number, name and suburb and these are separate fields and are stored as separate values in the database.
So, part of the project is to display all of the entries in a table, but combine the info from above (Street no, name, suburb) and display it together in one cell under the heading 'Address' so it shows as full address.
So my question is, what are some ways of doing this?
Everything is working so far and I can display each field individually, so it's just the question of formatting.
Hopefully, you guys can give me a hand,
Cheers!
EDIT: This is how I'm displaying the table right now:
$QueryResult = mysql_query("SELECT * FROM booking");
$row = mysql_fetch_row($QueryResult);
echo "<table width='100%' border='1'>";
echo "<tr><th>Booking No</th><th>Passanger Name</th><th>Phone</th><th>Unit</th>";
echo "<th>Street No</th><th>Street Name</th><th>Pick-up Suburb</th><th>Destination Suburb</th>";
echo "<th>Pick-up Date</th><th>Pickup time</th></tr>";
$countRows = 0;
$tweight=0;
while($row){
$countRows++;
echo "<tr><td>{$row[0]}</td>";
echo "<td>{$row[1]}</td>";
echo "<td>{$row[2]}</td>";
echo "<td>{$row[3]}</td>";
echo "<td>{$row[4]}</td>";
echo "<td>{$row[5]}</td>";
echo "<td>{$row[6]}</td>";
echo "<td>{$row[7]}</td>";
echo "<td>{$row[8]}</td>";
echo "<td>{$row[9]}</td></tr>";
$row = mysql_fetch_row($QueryResult);
}
How would I use CONCAT in this case?
Thanks
In php:
$address = $row[4] . ' ' . $row[5] . ' ' . $row[6];
In mysql:
SELECT CONCAT(street_no, ' ', street_name, ' ', suburb) AS address FROM ...
Choose one of them and change this code depending on the variable names you have.

How to generate a state/province dropdown that selects the value in the database

So I have several places for country and state/province dropdowns. What I'd like to do is have a function for each of these and when I run my while loop on customer data from mysql I want to select the right by default from the data in the database.
Reason is right now the dropdowns default to the HTML selected one so if users don't change it to theirs again when they edit their account they re-save their info with the wrong state/country.
I'd also like to use this in several places so I want a big array of countries and states/provinces that loop.
Just looking for a hand or be pointed to where this has been done in a function already.
Thank you kindly.
$states - query with all states, $user_state - current user's state. So your user's choice will be selected by default.
<?php
echo '<select name="state">';
while ($state = mysql_fetch_assoc($states))
{
echo '<option value="' . $state['state'] . '"';
if ($user_state == $state['state'])
{
echo ' selected';
}
echo '>' . $state . '</option>';
}
echo '</select>';

Categories