mysql_fetch_array - If recommended == yes show it first - php

Building a page that outputs each result from a search based on location/distance/w.e however I want the first result to be a "recommended" page if it falls within the search parameters. Each entry in the database has a row "recommended" and either yes or no.
If its =="yes" then it changes the background to yellow however if could be the 3rd, 6th and 17th result in the list. I want it so it shows 1 recommended at the top of the search results and then the rest and normal.
Search results so far:
while($row = mysql_fetch_array( $result )) {
if ($row['recommended'] == "Yes") {
echo '<div id="results_box_site" style="background-color: #ffffdf;">';
}
else {
echo '<div id="results_box_site">';
}
echo '<img class="results_site_image" src="' . $row['site_logo'] . '" />';
echo '<h1><a href="site.php?id=' . $row['site_ID'] . '">' . $row['site_name'];
if ($row['recommended'] == "Yes") {
echo ' - Recommended site';
}
echo '</a></h1>';
echo '<h2>Next game date: ' . $row['next_game'] . '</h2>
<img src="images/rating/5star.png" /> (rating 5/5 - 2 reviews)
<p id="results_box_content">';
echo $row['short_desc'];
echo '<br/><br/> >> More information <<
</p>
<h3 id="results_box_content">Facilities</h3>
<div id="details">
<ul>';
echo '<li>' . $row['lunch'] . '</li>';
echo '<li>' . $row['shop'] . '</li>';
echo '<li>' . $row['toilets'] . '</li>';
echo '<li>Minimum age: ' . $row['min_age'] . '</li>';
echo '<li>Game Type: ' . $row['game_type'] . '</li>';
echo '<li>Site Type: ' . $row['site_type'] . '</li>';
echo '<li>' . $row['price'] . '</li>';
echo '<li>' . $row['packages'] . '</li>';
echo '</ul>
</div>
</div>';
}
Messy code I know but any help would be appreaciated

You want to do this in your query. Add this to the end of your query:
ORDER BY `recommended` DESC
It will return the recommended ones first and then the non-recommended one.s
This assumes the valid values for that column is "Yes" and "No"

Related

Update a whole table with mysqli and php

I have created a page to list all users with their rights on the website. I used the code below to create and fill the table and it works great.
echo '<form method="post" id="detail" class="group" action="includes/setup_change.php?change=rights">';
echo '<div class="group">';
if (!empty($_GET['change'])) {
if ($_GET['change'] == 'rights') { echo '<span class="message_alert success"><span class="icon success"></span><span class="text">' . _('Your password was successfully changed') . '.</span></span>'; }
}
echo '<h2>' . _('Change') . ' ' . _('rights') . '</h2>';
// select database
mysqli_select_db( $mysqli, 'db_ccadmin' );
// check connection
if ( $mysqli->connect_errno > 0 ) {
trigger_error( _('Database connection failed') . ': ' . $mysqli->connect_error, E_USER_ERROR );
}
// sql query
$sql = "SELECT * FROM users";
$res = $mysqli->query( $sql );
if( !$res ) {
trigger_error( _('Wrong') . ' SQL: [' . $sql . ']. ' . _('Error') . ' : [' . $mysqli->error . ']' );
} else {
echo '<table id="table_sort_no_search">';
echo '<thead><tr>
<th class="username">' . _('Username') . '</th>
<th class="readonly">' . _('Read-only') . '</th>
<th class="manage">' . _('Manage') . '</th>
<th class="admin">' . _('Admin') . '</th>
</tr></thead>';
echo '<tbody>';
// output query results
while($row = $res->fetch_assoc()) {
echo '<tr>';
echo '<td><input type="text" name="username" value="' . $row['username'] . '" readonly></td>';
echo '<td><label><input type="radio" class="rights" name="rights_' . $row['username'] . '" value="1" ' . (isset($row['rights']) ? (($row['rights'] == '1') ? 'checked="checked"' : '' ) : '') . '></label></td>';
echo '<td><label><input type="radio" class="rights" name="rights_' . $row['username'] . '" value="2" ' . (isset($row['rights']) ? (($row['rights'] == '2') ? 'checked="checked"' : '' ) : '') . '></label></td>';
echo '<td><label><input type="radio" class="rights" name="rights_' . $row['username'] . '" value="3" ' . (isset($row['rights']) ? (($row['rights'] == '3') ? 'checked="checked"' : '' ) : '') . '></label></td>';
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
// free results to free up some system resources
$res->free();
The trouble is updating the whole table in the database. I don't know how to do this. Is it even possible to update the whole table (as generated) at once? If yes, how?
What would be the code that I need to put in my setup_change.php?
It would be great if you could help me!
It is not clear what you want to update, but this is how you can update (example):
$mysqli->query("UPDATE birthday SET Manage = null");
Of course, this does not solve your problem, as there is infinite ways to update the whole table. What table do you want to update, what values do you want to set?

where to put class or style name

I want to add three class for bottom of pagination where show page number , I used that in PHP but I can't figure out where to put class name inside of php echo to make work... because i'm getting errors...
class="box_one" for ... << Prev Page
class="box_two" for ... echo $thenumrows or $i
class= "box_three" ... Next Page >>
here is code...
<?php
if ($thenumrows != "")
{
echo "<br>";
if ($thecurrentpage > 1)
echo "<a href='". $_SERVER['PHP_SELF'] . "?cat= ". $theselectedcat ."&rows= "
. $thenumrows ."&page=". $thepreviouspage ."'>
<< Prev Page </a> "; for ($i=1;$i<=$totalpages;$i++)
{
if ($i == $thecurrentpage)
echo $i ." ";
else
echo "<a href='". $_SERVER['PHP_SELF'] ."?cat=". $theselectedcat ."&rows=".
$thenumrows ."&page=". $i ."'>$i</a> ";
}
if ($thecurrentpage < $totalpages)
echo "<a href='". $_SERVER['PHP_SELF'] ."?cat=". $theselectedcat ."&rows=".
$thenumrows ."&page=". $thenextpage ."'>Next Page >></a> ";
}
?>
please help thanks.
AM
I had to clean up your code. The classes are added to the a-tags:
if ($thenumrows != "") {
echo "<br />";
// Back
if ($thecurrentpage > 1) {
echo '<a class="box_one" href="'. $_SERVER['PHP_SELF'] . '?cat=' . $theselectedcat . '&rows=' . $thenumrows . '&page=' . $thepreviouspage . '"><< Prev Page</a> ';
}
// Paginating
for ($i=1; $i<=$totalpages; $i++) {
if ($i == $thecurrentpage) {
echo '<span class="box_two">' . $i .'</span> ';
}
else {
echo '<a class="box_two" href="' . $_SERVER['PHP_SELF'] . '?cat=' . $theselectedcat . '&rows=' . $thenumrows . '&page=' . $i . '">' . $i . '</a> ';
}
}
// Next
if ($thecurrentpage < $totalpages) {
echo '<a class="box_three" href="' . $_SERVER['PHP_SELF'] . '?cat=' . $theselectedcat . '&rows=' . $thenumrows . '&page=' . $thenextpage .'">Next Page >></a> ';
}
}
?>

PHP query - on 1st, 5th, 9th record add a class

Im wondering if its possible with PHP to add a class to X record returned. I know I can do this with JS only I'd like it to have the class added as the records are returned.
I have the following loop in my PHP, From what I've found in google I need to add a counter to do this only I've been unsuccessful so far...
while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
echo '<div class="entry span3"><span class="name">' . $row['First_Name'] . ' ' . $row['Surname'] . "</span>";
echo '<img src="' . $row["picture_1"] . '" alt="' . $row['First_Name'] . ' ' . $row['Surname'] . ', text ' . $row['Date'] . ' ">';
echo '<span class="from">seen in ' . ucwords($row["Location_County__Seen"]) . '</span>View Profile</div>';
}
In front of your while, add $c = 1
Before the end of your while loop, add $c++;
Then, modify your first line:
echo '<div class="entry span3"><span class="name">'
To
echo '<div class="entry span3';
if (($c % 4) == 1) echo ' newclassname ';
echo '"><span class="name">'
For the final result:
$c = 1;
while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
echo '<div class="entry';
if (($c % 4) == 1) echo ' newclassname ';
echo ' span3"><span class="name">' . $row['First_Name'] . ' ' . $row['Surname'] . "</span>";
echo '<img src="' . $row["picture_1"] . '" alt="' . $row['First_Name'] . ' ' . $row['Surname'] . ', text ' . $row['Date'] . ' ">';
echo '<span class="from">seen in ' . ucwords($row["Location_County__Seen"]) . '</span>View Profile</div>';
$c++;
}

How to sort from for each in PHP

From an array I start for each entry a function with a argument from the array (its friends from facebook). The function returns either 0 or a number bigger than 0. So at the moment, it justs displays it all mixed up, but I want it to show first all entries which get the result bigger than 0 from the function and then all, which are 0. I've no idea, how to do this.
This is my current code.
foreach ($friends as $key=>$value) {
//removed unimportant things
$friendresult = friendscore($id = $fvalue[id]);
if ($friendresult == "0") {
echo '<li>';
echo '<a class="box" href="javascript:invite('. $fvalue[id] . ')">';
echo '<img src="https://graph.facebook.com/' . $fvalue[id] . '/picture" title="' . $fvalue[name] . '" />';
echo '<label>' . $fvalue[name] . '</label>';
echo '<b>Invite</b>';
echo '</a>';
echo '</li>';
}
if ($friendresult !=="0"){
echo '<li>';
echo '<a class="box">';
echo '<img src="https://graph.facebook.com/' . $fvalue[id] . '/picture" title="' . $fvalue[name] . '" />';
echo '<label>' . $fvalue[name] . '</label>';
echo '<div class="totaltext">Score:'.$friendresult. '</div>';
echo '</a>';
echo '</li>';
}}
Edit: Found solution. Sometimes its too simple.
if ($friendresult == "0") {
$friend.= '<li>';
$friend.= '<a class="box" href="javascript:invite('. $fvalue[id] . ')">';
$friend.= '<img src="https://graph.facebook.com/' . $fvalue[id] . '/picture" title="' . $fvalue[name] . '" />';
$friend.= '<label>' . $fvalue[name] . '</label>';
$friend.= '<b>Invite</b>';
$friend.= '</a>';
$friend.= '</li>';
}
if ($friendresult !="0"){
$nofriend.= '<li>';
$nofriend.= '<a class="box">';
$nofriend.= '<img src="https://graph.facebook.com/' . $fvalue[id] . '/picture" title="' . $fvalue[name] . '" />';
$nofriend.= '<label>' . $fvalue[name] . '</label>';
$nofriend.= '<div class="totaltext">Score:'.$friendresult. '</div>';
$nofriend.= '</a>';
$nofriend.= '</li>';
}
echo $friend.$nofriend;

Create rows and columns based on COUNT query

Simple question I believe for anyone with minimal php skills (which I don't have sufficient amounts of haha)
$numrows = $retour['nb'] / 4;
echo $numrows;
echo "<table><tr>";
while ($callback = mysql_fetch_assoc($queryLocations2))
{
echo utf8_encode('<td><img src="/flags/' . strtolower($callback['loc_code']) . '.png" id="' . $callback['loc_id'] . '"><input type="checkbox" value="' . $callback['loc_url'] . '" />' . $callback['loc_city'] . ', ' . utf8_encode($callback['loc_state']) . '</td>');
}
echo "</tr></table>";
}
How would I go about presenting a table that will hold 4 results(4 columns) per row, based on the value of $numrows?
Thank you!
Output tr tags inside while loop:
$count = 0;
echo "<table>";
while ($callback = mysql_fetch_assoc($queryLocations2))
{
if ($count % 4 == 0)
echo '<tr>';
$count++;
echo utf8_encode('<td><img src="/flags/' . strtolower($callback['loc_code']) . '.png" id="' . $callback['loc_id'] . '"><input type="checkbox" value="' . $callback['loc_url'] . '" />' . $callback['loc_city'] . ', ' . utf8_encode($callback['loc_state']) . '</td>');
if ($count % 4 == 0)
echo '</tr>';
}
if ($count % 4 != 0)
{
// need to add missing td-s here
echo '</tr>';
}
echo "</table>";
$numrows = floor($retour['nb'] / 4);
echo $numrows;
$i=0;
echo "<table>";
while ($callback = mysql_fetch_assoc($queryLocations2))
{
if($i==4)
{
echo "<tr>";
echo utf8_encode('<td><img src="/flags/' . strtolower($callback['loc_code']) . '.png" id="' . $callback['loc_id'] . '"><input type="checkbox" value="' . $callback['loc_url'] . '" />' . $callback['loc_city'] . ', ' . utf8_encode($callback['loc_state']) . '</td>');
echo "</tr>";
$i=0;
}
$i++;
}
while ($i<4)
{
echo '<td></td>';
$i++;
}
echo "</table>";
}
//if numrows used for # of rows then use following
$count=0;
while ($callback = mysql_fetch_assoc($queryLocations2) && $count<=$numrows)
{
if($i==4)
echo "<tr>";
echo utf8_encode('<td><img src="/flags/' . strtolower($callback['loc_code']) . '.png" id="' . $callback['loc_id'] . '"><input type="checkbox" value="' . $callback['loc_url'] . '" />' . $callback['loc_city'] . ', ' . utf8_encode($callback['loc_state']) . '</td>');
if($i==4)
{
echo "</tr>";
$i=0;
$count++;
}
$i++;
}
while ($i<4)
{
echo '<td></td>';
$i++;
}
echo "</table>";
}

Categories