How would I do let the user know if there wasnt any rows found?
And if it finds (for example) 26 rows I want it to print that it found 26 hits (Your search for "hey" gave 26 results)
$name = mysql_real_escape_string($_POST['player_name']);
$q = mysql_query("SELECT * FROM players WHERE name LIKE '%$name%'");
while ($row = mysql_fetch_array($q)) {
$name = $row['name'];
echo "<ul>\n";
echo "<li>" . "" .$name . " </li>\n";
echo "</ul>";
}
Use mysql_num_rows:
$rowCount = mysql_num_rows($q);
if ($rowCount === 0)
{
echo 'Found nothing!';
}
else
{
echo "Found $rowCount rows!";
while ($row = mysql_fetch_array($q))
{
$name = $row['name'];
echo "<ul>\n";
echo "<li>" . "" .$name . " </li>\n";
echo "</ul>";
}
}
Check out mysql_num_rows
mysql_num_rows
$numberOfResults = mysql_num_rows($q);
Related
How i separate the first result of for each loop and remaining. I have 2 divs, i want first result to be displayed there and rest on another div.
Also is there any way that i can get json decode without for each loop, i want to display result based on for each values from database, and querying database in for each loop is not recommended.
Here is my code, What i want
<div class="FirstDiv">
Result1
</div>
<div class="RemDiv">
Remaining result from for each loop
</div>
Here is full code
$data = json_decode($response->raw_body, true);
$i = 0;
foreach($data['photos'][0]['tags'][0]['uids'] as $value) {
if (++$i == 6)
break;
$check = "SELECT fullname FROM test_celebrities WHERE shortname = '$value[prediction]'";
$rs = mysqli_query($con,$check);
if (mysqli_num_rows($rs)==1) //uid found in the table
{
$row = mysqli_fetch_assoc($rs);
$fullname= $row['fullname'];
}
echo 'Celebrity Name: ' . $fullname . '<br/>';
echo 'Similar: ' . $value['confidence']*100 .'%'. '<br/><br/>';
echo "<img src='actors/$value[prediction].jpg'>";
echo "<hr/>";
}
Try this:
$data = json_decode($response->raw_body, true);
$i = 0;
echo '<div class="FirstDiv">'; // add this line here
foreach( $data['photos'][0]['tags'][0]['uids'] as $value ) {
if (++$i == 6) break;
$check = "SELECT fullname FROM test_celebrities WHERE shortname = '$value[prediction]'";
$rs = mysqli_query($con,$check);
if ( mysqli_num_rows($rs) == 1 ) { //uid found in the table
$row = mysqli_fetch_assoc($rs);
$fullname= $row['fullname'];
}
// Echo celebrity information:
echo 'Celebrity Name: ' . $fullname . '<br/>';
echo 'Similar: ' . $value['confidence']*100 .'%'. '<br/><br/>';
echo "<img src='actors/$value[prediction].jpg'>";
echo "<hr/>";
if ($i==1) { echo '</div><div class="RemDiv">'; }; // add this line here
}
echo '</div>'; // close the last tag
$predictions=array();
foreach($data['photos'][0]['tags'][0]['uids'] as $value) {
$predictions[]="'" . mysqli_real_escape_string($con, $value[prediction]) . "'";
}
$check="SELECT fullname FROM test_celebrities WHERE shortname IN (" . implode(',' $predictions) . ")";
$rs = mysqli_query($con,$check);
while ($row = mysqli_fetch_assoc($rs)) {
if (!$count++) {
// this is the first row
}
But note that you now have two sets of data which are sorted differently - hence you'll need to iterate through one and lookup values in the other.
I'm having trouble trying to get a nested < ul > inside of a while loop. I'm not sure if this even possible so i'm open to alternatives.
Here's a quick image of what my database looks like and what i'm trying to achieve.
Here's my sql
SELECT *
FROM drinks_category, drinks_lookup, drinks
WHERE drinks.drink_id = drinks_lookup.drink_id
AND drinks_lookup.drinks_category_id = drinks_category.drinks_category_id
ORDER BY drinks_category.drinks_category_title
Here's my output php
$result = $conn->query($sql) or die(mysqli_error());
$last_category = 0;
while ($row = $result->fetch_assoc()) {
if($row['drinks_category_id'] != $last_category) {
echo "<h1>" . $row['drinks_category_title'] . "</h1>";
}
echo "<p>" . $row['drink_name'] . "</p>";
$last_category = $row['drinks_category_id'];
}
Im using mysqli and php. Thanks in advance!
Update your while loop to the following:
while ($row = $result->fetch_assoc()) {
if($row['drinks_category_id'] != $last_category) {
if($last_category != 0) echo '</ul>';
echo "<h1>" . $row['drinks_category_title'] . "</h1>";
echo "<ul>";
}
echo "<li>" . $row['drink_name'] . "</li>";
$last_category = $row['drinks_category_id'];
}
if($last_category != 0) echo "</ul>";
Instead of having 3 tables, you could just add a category_id to your drinks table, just to simplify things. Then you can do this:
SELECT drink_name, (SELECT drnk_category_title FROM drinks_category WHERE drink_category_id = drink_category // THE COLUMN I SUGGESTED //) AS title FROM drinks
And then you can loop the result and build the nodes you desire really easily
$result = $conn->query($sql) or die(mysqli_error());
$last_category = 0;
while ($row = $result->fetch_assoc()) {
if($row['drinks_category_id'] != $last_category) {
// close previous </ul>
if( $last_category != 0 ) echo "</ul>";
// new title
echo "<h1>" . $row['drinks_category_title'] . "</h1>";
// new <ul>
echo "<ul>";
$last_category = $row['drinks_category_id'];
}
echo "<li>" . $row['drink_name'] . "</li>";
}
// close last </ul>
if( $last_category != 0 ) echo "</ul>";
I am trying to display data in table form with 3 columns. Each should have a main category with some drop down lists. I get all the information to display but all is in one column and the drop down information does not display with the correct heading.
echo "<table>";
while ($row = mysql_fetch_array($result)) {
$count = 1;
if ($count = 1) {
$sCatID = ($row['CatID']);
echo "<tr valign='top'><td><b><a href='#" . $sCatID . "'>" . $sCatID . "</a></b><br>";
// column 1 categories
$result2 = mysql_query("SELECT * FROM test_prefixSubCat WHERE CatID=$sCatID");
// sub-cats
while ($row2 = mysql_fetch_array($result2)) {
$sSub = ($row2['CatID']);
$sSubID = ($row2['SubID']);
echo "<dd><a href='#'>" . $sSub . "</a><br>";
echo "</td>";
}
$count = 2;
} elseif ($count = 2) {
$sCatID = ($row['CatID']);
echo "<td><b><a href='.$sCatID.'>" . $sCatID . "</a></b><br>";
// column 1 categories
$result2 = mysql_query("SELECT * FROM test_prefixSubCat WHERE CatID=$sCatID");
// sub-cats
while ($row2 = mysql_fetch_array($result2)) {
$sSub = ($row2['CatID']);
$sSubID = ($row2['SubID']);
echo "<dd><a href='#'>" . $row2['Sub'] . "</a><br>";
echo "</td>";
}
$count = 3;
} elseif ($count = 3) {
$sCatID = ($row['CatID']);
echo "<td><b><a href='.$sCatID.'>" . $sCatID . "</a></b><br>";
// column 1 categories
$result2 = mysql_query("SELECT * FROM test_prefixSubCat WHERE CatID=$sCatID");
// sub-cats
while ($row2 = mysql_fetch_array($result2)) {
$sSub = ($row2['CatID']);
$sSubID = ($row2['SubID']);
echo "<dd><a href='.$sSub.'>" . $sSub . "</a><br>";
echo "</td></tr>";
}
$count = 1;
}
}
if ($count = 2) {
echo "<td> </td><td> </td></tr>";
} elseif ($count = 3) {
echo "<td> </td></tr>";
}
echo "</table>";
It doesn't seem to close the rows and table correctly... And it is also putting some of the drop down items before it displays the first heading.
If i display it in only one column it is working fine.
You should use == instead of single = in your if statements. Else it would execute everytime as that condition is always true.
I'm trying to write a relatively simple twitter query in php with result like this:
from:TravisBenjamin3 OR from:AshleyElisaG OR from:tncvaan ...
This code works accept it doesn't echo the second OR, also I don't want to display an OR on the last row.
$counter = 0;
while($row = mysql_fetch_array($result)){
$counter++;
echo " from:";
echo $row['twitter'];
if ($counter < count($row)) {
echo " OR";
}
}
Some help would be greatly appreciated. Thanks,
<?php
$tweets = array();
while($row = mysql_fetch_array($result)) {
$tweets[] = $row['twitter'];
}
if(count($tweets) > 0) {
echo "from:" . implode(" OR from:", $tweets);
}
?>
$rowCount = mysql_num_rows($result);
while($row = mysql_fetch_array($result)){
$counter++;
echo " from:";
echo $row['twitter'];
if ($counter < $rowCount) {
echo " OR";
}
}
$count = mysql_num_rows($result);
for($r = 0; $r < $count; $r++)
{
$row = mysql_fetch_array($result));
echo " from:";
echo $row['twitter'];
if ($r < ($count - 1))
{
echo " OR";
}
}
Or, even easier:
$search = "";
while($row = mysql_fetch_array($result))
{
$search .= " from:" . $row['twitter'] . " OR";
}
// Cut the last " OR"
$search = substr($search, 0, strlen($search) - 3);
echo $search;
Which is actually a poor man's implementation for implode() suggested by Aaron W..
At the moment I'm using a WHERE loop to display 2 columns of information in my database. Here's the code:
$sql2 = sprintf($rawsql2, mysql_real_escape_string($id));
$result2 = mysql_query($sql2);
/*These if & while statements decide whether or not the information should be displayed. If no results are returned from the query above then an alternative message is shown.*/
if (mysql_num_rows($result2) > 0) {
while ($row = mysql_fetch_array($result2, MYSQL_ASSOC)) {
echo $row["open"] . "-" . $row["close"] . "<br/><br/>";
}
}
else {
echo "Error";
}
This outputs the following:
08:00:00-12:30:00
13:30:00-16:30:00
09:00:00-17:00:00
18:00:00-20:00:00
09:00:00-17:00:00
18:00:00-20:00:00
08:00:00-17:00:00
18:00:00-20:00:00
09:00:00-17:00:00
18:00:00-20:00:00
Now, what I need to do is group every 2 lines that are turned so instead it would look like:
08:00:00-12:30:00 13:30:00-16:30:00
09:00:00-17:00:00 18:00:00-20:00:00
09:00:00-17:00:00 18:00:00-20:00:00
08:00:00-17:00:00 18:00:00-20:00:00
09:00:00-17:00:00 18:00:00-20:00:00
Is this possible? Thanks for any help
edit: Thanks for all the answers everyone...I used Shakti's answer as it was the first one I saw but by the looks of it everyone's are pretty similar.
if(mysql_num_rows($result2) > 0)
{
$count=0;
while ($row = mysql_fetch_array($result2, MYSQL_ASSOC))
{
echo $row["open"] . "-" . $row["close"] ;
if ($count % 2 !=0 )
{
echo "<br/><br/>";
}
$count++;
}
}
try this:
$lines = 1;
if(mysql_num_rows($result2) > 0) {
while ($row = mysql_fetch_array($result2, MYSQL_ASSOC)) {
echo $row["open"] . "-" . $row["close"];
echo (($lines%2 == 0)?"<br/><br/>":'');
$lines++;
}
} else {
echo "Error";
}
if(mysql_num_rows($result2) > 0){
$counter = 0;
while($row = mysql_fetch_array($result2, MYSQL_ASSOC)){
echo $row["open"] . "-" . $row["close"] . " ";
if(++$counter % 2 == 0){
echo "<br/><br/>";
$counter = 0;
}
}
}
if($result2) {
$printTimes = function($r) {echo $row["open"] . "-" . $row["close"];};
$newLine = false;
while ($row = mysql_fetch_assoc($result2))
{
$printTimes($row);
echo $newLine ? '<br /><br />' : ' ';
$newLine = !$newLine;
}
}
My version of code.
$sql2 = sprintf($rawsql2, mysql_real_escape_string($id));
$result2 = mysql_query($sql2);
if(mysql_num_rows($result2) > 0) {
while (true) {
$row1 = mysql_fetch_array($result2, MYSQL_ASSOC);
$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
if (!($row1 and $row2)) break;
echo $row1["open"] . "-" . $row1["close"] . " ";
echo $row2["open"] . "-" . $row2["close"] . "<br/><br/>";
}
} else {
echo "Error";
}
$i=1;
while ($row = mysql_fetch_array($result2, MYSQL_ASSOC)) {
if ($i == 1) {
echo $row["open"] . "-" . $row["close"];
$i++;
} else {
echo $row["open"] . "-" . $row["close"] . "<br /><br />";
$i=1;
}
Output <br/><br/> only on every other iteration of the loop. (BTW, a <p> or <ul> or <div> would be more semantic.)
$counter = 0;
while ($row = mysql_fetch_array($result2, MYSQL_ASSOC)) {
echo $row["open"] . "-" . $row["close"] . " ";
// Break line after every two items
$counter = ($counter + 1) % 2;
if ($counter == 0)
echo "<br/><br/>";
}
$sql2 = sprintf($rawsql2, mysql_real_escape_string($id));
$result2 = mysql_query($sql2);
/*These if & while statements decide whether or not the information should be displayed. If no results are returned from the query above then an alternative message is shown.*/
int row_count=mysql_num_rows($result2)
if(row_count > 0) {
while ($row = mysql_fetch_array($result2, MYSQL_ASSOC)) {
row_count--;
echo $row["open"] . "-" . $row["close"] ;
if(row_count > 0 && ($row = mysql_fetch_array($result2, MYSQL_ASSOC)){
row_count--;
echo $row["open"] . "-" . $row["close"] ;
}
"<br/><br/>"
}
} else {
echo "Error";
}
should do it, u may need to catch if there is an odd number