I am looking to query my database based on what the user would like to search by: Ex. (Zip, City, State, Name, Etc Etc). Let's say I search by zip code, and there are 2 results in my SQL for that zip code.
I would like the results to be displayed in the following format on my webpage:
Company Name
Address
City, State Zip
Contact Us Link Link to Another Page
I use the following code for something similar but I display the results in a table with check-boxes.
<?php
while ($row = mysql_fetch_assoc($result))
{
echo '<tr><td>';
echo '<input type="checkbox" name="selected[]" value="'.$row['order_number'].'"/>';
echo '</td>';
foreach ($row as $key => $value)
echo '<td>'.htmlspecialchars($value).'</td>';
echo '</tr>';
}
?>
I really would like to display the results of the search in the format I explained above. I am lost on how to get the results I want. Any help is much appreciated!!
Thanks
Would it be possible to do this as well:
If the Company Name row in my SQL is blank/empty, skip over that row/entry and go to the next.
What you could do, is pull each piece of information individually from the resulting row (change the fields based on what you pull from the table):
while($row = mysql_fetch_assoc($result))
{
echo htmlspecialchars($row['comp_name']) . '<br />';
echo htmlspecialchars($row['Address']) . '<br />';
echo $row['city'] . ', ' . $row['zip'] . '<br />';
echo 'Contact usSome link';//If it's an email
}
or for table output
while($row = mysql_fetch_assoc($result))
{
echo '<table>'
echo '<tr><td colspan="2">' . htmlspecialchars($row['comp_name']) . '</td></tr>';
echo '<tr><td colspan="2">' . htmlspecialchars($row['Address']) . '</td></tr>';
echo '<tr><td colspan="2">' . $row['city'] . ', ' . $row['zip'] . '<br />';
echo '<tr><td>Contact us</td><td>a href="index.php">Some linke</a></td></tr>';
echo '</table>';
}
Since you are wanting to list the information, you could use an <ul> and output each row as <li>.
<style>
ul { list-style: none; }
</style>
<?php
while ($row = mysql_fetch_array($result)){
echo "<ul>";
echo "<li>".$row["companyName"]."</li><li>".$row["address"]."</li><li>".$row["city"].", ".$row["state"]." ".$row["zipcode"]."</li>";
echo "<li>Contact Us</li><ul>";
}
?>
Also, what is the "Link to Another page"?
Related
PHP failed to print data of two columns of a mysql table,the result only shows the first row of data which proved to be right,with the rest rows show
" Undefined offset".
$query1='select MNO from mima01 order by MNO;';
$query2='select MTYPE from mima01 order by MNO;';
$result1=mysqli_query($conn,$query1);
$result2=mysqli_query($conn,$query2);
$username=mysqli_fetch_array($result1);
$usertype=mysqli_fetch_array($result2);
$count='select count(*) from mima01;';
$usercount=mysqli_query($conn,$count);
$usernum=mysqli_fetch_array($usercount);
for($i=0;$i<$usernum[0];$i++) {
echo '<tr><td style="text-align:center">' ;
echo $username[$i];
echo '</td>';
echo '<td style="text-align:center">' ;
echo $usertype[$i];
echo '</td></tr>';
}
I've rewritten your code to the normal way you would walk through a query result:
$query = 'select MNO, MTYPE from mima01 order by MNO;';
$result = mysqli_query($conn, $query);
while ($data = mysqli_fetch_array($result)) {
echo '<tr><td style="text-align:center">' .
$data['MNO'] .
'</td>' .
'<td style="text-align:center">' .
$data['MTYPE'] .
'</td></tr>';
}
Please note that I have no way to run this code so I can't be a 100% sure it will work.
from what I understand I think your solution is:
$query='SELECT MNO,MTYPE FROM mima01 ORDER BY MNO DECS;'; //use DECS|ASC
$result=mysqli_query($conn,$query);
$username=mysqli_fetch_array($result);
// to count the rows
$count=mysqli_num_rows($result);;
echo $count;
// to display data
while($row=mysqli_fetch_array($result) {
echo '<tr><td style="text-align:center">';
echo $row['MNO'];
echo '</td>';
echo '<td style="text-align:center">';
echo $row['MTYPE'];
echo '</td></tr>';
}
Hope it was helpful
So in this part of the website i'm making an edit person information page and if there are more than one person with the name searched you get a table with all the persons; you choose the needed one and edit it in another page.
I need to pass the array that matches with the person selected. I don't know how to pick the array in the other page through POST. This is a part of code of the page that sends the array:
$squery=mysqli_query($conn,"SELECT * FROM amico WHERE (Nome= '" .
$nome . "') AND (Cognome ='" .$cognome. "')");
$num = mysqli_num_rows($squery);
$i=0;
$array= array();
while($rowa=mysqli_fetch_assoc($squery)){
$array[$i]=$rowa;
$i++;
}
$ssquery=mysqli_query($conn,"SELECT * FROM amico WHERE (Nome= '" .
$nome . "') AND (Cognome ='" .$cognome. "')");
if($num > 1) {
echo 'Trovato più di un elemento';
echo '<table border="1">';
echo '<tr>';
echo '<td><p>S</p></td>';
echo '<td><p>Nome</p></td>';
echo '<td><p>Cognome</p></td>';
echo '<td><p>Città</p></td>';
echo '</tr>';
$i=0;
echo '<form method="POST" action="moficatr.php">';
while ($row = mysqli_fetch_array($ssquery)) {
echo '<tr>';
echo '<td> <p> <input type="Radio" name="persona" value="'.
$rowa[$i] . '"></p></td>';
echo ' <td><p>' .$row['Nome'] . '</p></td>';
echo '<td><p>'.$row['Cognome'].'</p></td>';
echo '<td><p>'.$row['Citta'].'</p></td>';
echo '</tr>';
$i++;
}
echo '</table>';
echo '<br><br><input type="submit" value="Modifica"></form>';
}
You can simply pass the selected user's ID to the next page and then fetch all the details using the ID from that page instead of sending the whole array of data to the other side.
Why are you executing the same query twice in your code ? you only need one query which will give you all the data.
Also I strongly encourage you to use PDO please - http://php.net/manual/en/book.pdo.php Or at least prepared statments which is supported in MySQLi as well
$ssquery=mysqli_query($conn,"SELECT * FROM amico WHERE (Nome= '" .
$nome . "') AND (Cognome ='" .$cognome. "')");
$num = mysqli_num_rows($ssquery);
if($num > 1) {
echo 'Trovato più di un elemento';
echo '<table border="1">';
echo '<tr>';
echo '<td><p>S</p></td>';
echo '<td><p>Nome</p></td>';
echo '<td><p>Cognome</p></td>';
echo '<td><p>Città</p></td>';
echo '</tr>';
echo '<form method="POST" action="moficatr.php">';
while ($row = mysqli_fetch_assoc($ssquery)) {
echo '<tr>';
echo '<td> <p> <input type="Radio" name="persona" value="'.
$row['id'] . '"></p></td>'; //you can change 'id' with your column name if it's different
echo ' <td><p>' .$row['Nome'] . '</p></td>';
echo '<td><p>'.$row['Cognome'].'</p></td>';
echo '<td><p>'.$row['Citta'].'</p></td>';
echo '</tr>';
$i++;
}
moficatr.php
//Set up a Mysql connection.
$user_id = (int) $_POST['persona'];
//Query the db to fetch all the details of this user.
$query = "SELECT * from your_table where id=?";
//Prepare statment and execute.
$stmt = mysqli_prepare($connection, $query);
mysqli_stmt_bind_param($stmt, "i", $user_id);
mysqli_stmt_execute($stmt);
/* bind result variables */
mysqli_stmt_bind_result($stmt, $name, $address,...);
echo $name;
echo $address;
I am trying to put extracted mysql data into a table using php. The issue is that I am able to put the fetched data into either all rows or all columns. What I would like to do is put the fetched data into a column format and as soon as 4 columns are used, the next data should start on the next row and so on.
Currently I am using the logic below, however I am getting everything in a column.
The goal is to limit the data to a page size so the data can be printed.
<div>
$result = mysql_query("SELECT * FROM master_data");
echo "<table border='1'>
<tr>
<th>Accounts</th>
</tr>";
echo "<tr>";
while($row = mysql_fetch_array($result))
{
echo "<td>";
echo "First Name:" . $row['First_Name'] . "</br>";
echo "Middle Name:" . $row['Middle_Name'] . "</br>";
echo "Last Name:" . $row['Last_Name'] . "</br>";
echo "</td>";
}
echo "</tr>";
echo "</table>";
mysql_close($con);
?></div>
If I understand your problem correctly you will want to do something like:
Keep a counter to see which number record you're looking at ($i)
$i = 0; // Your counter
while($row = mysql_fetch_array($result))
{
// ...
On every 4th iteration of the loop output something like </tr><tr>.
if ($i % 4 == 0 && $i > 0) // Will return true if `$i` is a multiple of 4 and greater than 0
{
echo "</tr><tr>"; // Output your HTML to start a new row
}
One final note: As others will be pointing out. You should avoid using the MySQL extension. You should use MySQLi or PDO instead.
<div>
$result = mysql_query("SELECT * FROM master_data");
echo "<table border='1'>
<tr>
<th>Accounts</th>
</tr>";
echo "<tr>";
$i = 1;
while($row = mysql_fetch_array($result))
{
if($i == 4)
{
echo "<td>";
echo "First Name:" . $row['First_Name'] . "</br>";
echo "Middle Name:" . $row['Middle_Name'] . "</br>";
echo "Last Name:" . $row['Last_Name'] . "</br>";
echo "</td>";
$i=0;
}
$i++;
}
echo "</tr>";
echo "</table>";
mysql_close($con);
?>
</div>
I am writing an application to build a chart using the google chart API.
The data is dynamic, and resides in a MySQL database.
The issue being faced is that the array that contains the datasource to be displayed only contains the first row returned by the query. Even after looping.
This is the code:
include 'connect.php';
$sql="SELECT result,COUNT(result) value
FROM test
WHERE result LIKE 'GY%'
GROUP BY result";
$result=mysqli_query($link,$sql);
$myurl[]="['Option','Value']";
while($row=mysqli_fetch_assoc($result))
{
$result=$row['result'];
$value=$row['value'];
$Title="My Results";
$myurl[]="['".$result."',".$value."]";
}
<script type="text/javascript">
google.load('visualization', '1', {'packages':['corechart']});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.arrayToDataTable([
<?php
echo (implode(",",$myurl));?>
]);
var options={
title:'<?php echo($Title);?> '
};
Only the first row being returned is used in the chart,therefore it shows that value as 100%. How can i ensure ALL the row values are used?
Modify the query like this
$sql="SELECT result value
FROM test
WHERE result LIKE 'GY%'
GROUP BY result";
$result=mysqli_query($link,$sql);
And the number of results you can access like below.
mysqli_num_rows($result)
If you want an associative array, use this:
$query = "SELECT column FROM `table` WHERE id='" . $id . "'"; / Whatever your query is
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
You can then call $row and the column name from the row with a numbered id that is $id by using $row['columnname']; I'm also pretty sure that you can use print_r($row['columnname']); without including the WHERE id='" . $id . "'"; in the query and instead just having $query = "SELECT column FROMtable; and it will print out all of the data (every row) in that column.
Also, if you're just looking to draw data to a chart, using a google API could over complicate things, as you could just output the data to an HTML table with this code:
<?php
$query = "SELECT FROM `table`";
$result = mysql_query($query);
echo '<table class="table">';
echo '<tr class="table">';
echo '<th class="table">Column</th>';
echo '</tr>';
while($row = mysql_fetch_assoc($result)
{
echo '<tr class="table">';
echo '<th class="table">' . $row['column'] . '</th>';
echo '</tr>';
}
echo '</table>';
?>
Add any other things you need using echo in php then the html code. You can also put buttons in the table, and pretty much anything else. This site should give any more info you would need on HTML tables: http://www.w3schools.com/html/html_tables.asp
Here's another little example I made of an HTML form echoed in php. It has buttons, etc.
$query = "SELECT * FROM `users` WHERE status='accepted'";
$result = mysql_query($query) or die(mysql_error());
echo '<table class="table">';
echo '<h2>Accounts</h2>';
echo '<tr class="table">';
echo '<th class="table">Name</th>';
echo '<th class="table">Email</th>';
echo '<th class="table">Username</th>';
echo '<th class="table">Date Created</th>';
echo '<th class="table">Decline</th>';
echo '<th class="table">Accept</th>';
echo '<th class="table">Give Admin</th>';
echo '</tr>';
while($row = mysql_fetch_assoc($result))
{
echo '<tr class="table">';
echo '<th class="table">' . $row['name'] . '</th>';
echo '<th class="table">' . $row['email'] . '</th>';
echo '<th class="table">' . $row['username'] . '</th>';
echo '<th class="table">' . $row['trn_date'] . '</th>';
echo '<form method="post">';
echo '<th class="table"><input type="submit" name="decline_' .$row["id"]. '" value="decline" ></input></th>';
echo '<th class="table"><input type="submit" name="accept_' .$row["id"]. '" value="accept" ></input></th>';
echo '<th class="table"><input type="submit" name="giveadmin_' .$row["id"]. '" value="accept/give admin" ></input></th>';
echo '</form>';
echo '</tr>';
}
echo '</table>';
Hope that helped:)
i'm using the following code to show clickable pictures, but underneath the picture it shows a description for the picture, in this part:
<br>".$row['optie'];"
but some lines in the table are too long to display the picture and description correctly next to each other.
So how can i show these descriptions and add a line break?
<?php
$query = "SELECT * FROM $tabel";
$result = mysql_query($query);
$aantal = 0;
echo "<table>";
echo "<tr>";
while( ($row = mysql_fetch_array($result)))
{
$src = $row['afbeelding'];
echo '<div class="Image">';
echo "<td align='center'><h2><a href='pagina3.php?lang=" . $_SESSION['lang'] . "&naam=" . $naam . "&postcodehuisnummer=" .$postcodehuisnummer ."&fietskeuze=" . $fietskeuze . "&opties=" . $row['optie'] . "&optieid=" . $row['opties_id'] . "' '><img src=".$src." width='400px'><br>".$row['optie'];"</a><br /><br /> ";
echo '</div>';
$aantal++;
if ($aantal==3) {echo "<tr>"; $aantal=0;}
}
echo "</tr></tr>";
echo "</table>";
?>
You should simply concatenate your value onto the end of your URL.
$row['optie'] = str_replace('\r', '<br>', $row['optie']);