I have this following piece of code
include_once("config/connection.php");
$sql = "SELECT * FROM qqqq WHERE LOWER(pdf_ad) LIKE '%" . $aranacak_metin . "%'";
$result = $DBcon->query($sql);
if ($result && is_array($result) && count($result) > 0) {
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$yeniad = seo($row['pdf_ad']);
echo '<form action="/indir/' . $yeniad . '/' . $row['pdf_liste_no'] . '" id="sorgu" method="post">';
echo '<li class="list-group-item">';
echo '<input type="hidden" name="id" value="' . $row['pdf_liste_no'] . '">';
echo '<input type="hidden" name="name" value="' . $row['pdf_ad'] . '">';
echo '<img alt="' . $row['pdf_ad'] . '" src="' . $row['pdf_resim'] . '" width=120" height="150"><a style="font-size:20px; text-decoration:none; color:black;" href="#"> ' . $row['pdf_ad'] . '</a> ';
echo '<button type="submit" class="btn btn-success">';
echo '<i class="fa fa-arrow-circle-right fa-lg"></i>';
echo '</button>';
echo '</li>';
echo '</form>';
}
} else {
echo $aranacak_metin;
}
if statement does its job if the result is set shows the rows but if there is no match in DB else statement
doesnt work am i missing something need your help thank you
update:it turns else everytime now
in current scenario $result is always set. so just check you got any match record or not, based on that put conditions as below.
if($result && $result->num_rows && $result->num_rows > 0){
} else {
}
if its PDO query then
if($result && is_array($result) && count($result) > 0){
} else {
}
Try below code:
if ( is_array($result) && count($result) > 0 ) {
// staff
} else {
echo $aranacak_metin;
}
Changed my php version it worked ?
Related
This code added currency switcher to a menu,
But unfortunately I don't have access to MySQL database so I can't add image "flag" to each currency, then execute them
So I tried to use if statement.
This is my code:
$currency_switcher_enable = houzez_option('currency_switcher_enable');
$is_multi_currency = houzez_option('multi_currency');
if( $currency_switcher_enable != 0 && $is_multi_currency != 1 ) {
if (class_exists('FCC_Rates')) {
$supported_currencies = houzez_get_list_of_supported_currencies();
if (0 < count($supported_currencies)) {
$current_currency = houzez_get_wpc_current_currency();
echo '<li class="btn-price-lang btn-price">';
echo '<form id="houzez-currency-switcher-form" method="post" action="#" class="open">';
echo '<button id="houzez-selected-currency" class="btn dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"><span>' . $current_currency . '</span> <i class="fa fa-sort"></i></button>';
echo '<ul id="houzez-currency-switcher-list" class="dropdown-menu" aria-labelledby="dropdown" style="display:none;">';
foreach ($supported_currencies as $currency_code) {
echo '<li data-currency-code="' . $currency_code . '">' . $currency_code . '</li>';
if (data-currency-code='EUR') {
echo '<img src="images/euro-flag.png"';
if (data-currency-code='TR') {
echo '<img src="images/turkish-flag.png"';
}
}
echo '</ul>';
echo '<input type="hidden" id="houzez-switch-to-currency" name="houzez_switch_to_currency" value="' . $current_currency . '" />';
echo '<input type="hidden" id="currency_switch_security" name="nonce" value="' . wp_create_nonce('houzez_currency_converter_nonce') . '"/>';
echo '</form>';
echo '</li>';
}
}
}
?>
But it doesn't work? What I'm doing wrong?
This is the original code:
$currency_switcher_enable = houzez_option('currency_switcher_enable');
$is_multi_currency = houzez_option('multi_currency');
if( $currency_switcher_enable != 0 && $is_multi_currency != 1 ) {
if (class_exists('FCC_Rates')) {
$supported_currencies = houzez_get_list_of_supported_currencies();
if (0 < count($supported_currencies)) {
$current_currency = houzez_get_wpc_current_currency();
echo '<li class="btn-price-lang btn-price">';
echo '<form id="houzez-currency-switcher-form" method="post" action="#" class="open">';
echo '<button id="houzez-selected-currency" class="btn dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"><span>' . $current_currency . '</span> <i class="fa fa-sort"></i></button>';
echo '<ul id="houzez-currency-switcher-list" class="dropdown-menu" aria-labelledby="dropdown" style="display:none;">';
foreach ($supported_currencies as $currency_code) {
echo '<li data-currency-code="' . $currency_code . '">' . $currency_code . '</li>';
}
echo '</ul>';
echo '<input type="hidden" id="houzez-switch-to-currency" name="houzez_switch_to_currency" value="' . $current_currency . '" />';
echo '<input type="hidden" id="currency_switch_security" name="nonce" value="' . wp_create_nonce('houzez_currency_converter_nonce') . '"/>';
echo '</form>';
echo '</li>';
}
}
}
?>
You have many fails here:
The first is as #kerbh0lz mentioned, the "=" and "==" is not the same purpose. here yours is wrong.
Second your second currency If is in the 1st Currency If so it wont ever pass by.
Third you are comparing a string without quote or a var without $ before data-currency-code so instead you should use $currency_code
Try this:
foreach ($supported_currencies as $currency_code) {
echo '<li data-currency-code="' . $currency_code . '">' . $currency_code . '</li>';
if ($currency_code =='EUR')
echo '<img src="images/euro-flag.png"';
elseif ($currency_code == 'TR')
echo '<img src="images/turkish-flag.png"';
}
I am trying to put some php code in a code that is combined with HTML.
I have the following code:
<?php
$result = mysqli_query($db,"SELECT * FROM klassen");
while($record = mysqli_fetch_array($result)) {
echo '<div class="groepitem"><input type="radio" [IN THIS PLACE] name="groep" value="' . $record['klasNaam'] . '">' . $record['klasNaam'] . '</div>';
}
?>
On the [in this place] spot I want to put the following code:
if($_SESSION['gebruikers_klasid'] == $record['klas_id']) {
echo 'checked';
}
I have tried it multiple times but I just can't get it to work.
You can concatenate a ternary operator like this:
$result = mysqli_query($db, "SELECT * FROM klassen");
while($record = mysqli_fetch_array($result)) {
echo '<div class="groepitem"><input type="radio" ' . ($_SESSION['gebruikers_klasid'] == $record['klas_id'] ? "checked" : "") . ' name="groep" value="' . $record['klasNaam'] . '">' . $record['klasNaam'] . '</div>';
}
You can do this many different ways, but if you want cleaner html code:
<?php
$result = mysqli_query($db,"SELECT * FROM klassen");
while ($record = mysqli_fetch_array($result)) {
if ($_SESSION['gebruikers_klasid'] == $record['klas_id']) {
$checked = ' checked';
} else {
$checked = '';
}
echo '<div class="groepitem"><input type="radio"' . $checked . ' name="groep" value="' . $record['klasNaam'] . '">' . $record['klasNaam'] . '</div>';
}
?>
OR...
<?php
$result = mysqli_query($db,"SELECT * FROM klassen");
while ($record = mysqli_fetch_array($result)) {
$checked = ($_SESSION['gebruikers_klasid'] == $record['klas_id']) ? ' checked' : '';
echo '<div class="groepitem"><input type="radio"' . $checked . ' name="groep" value="' . $record['klasNaam'] . '">' . $record['klasNaam'] . '</div>';
}
?>
Another way is to exit your echo then run the conditional statement then resume your echo... eg
<?php
$result = mysqli_query($db,"SELECT * FROM klassen");
while($record = mysqli_fetch_array($result)) {
echo '<div class="groepitem"><input type="radio" ';
if($_SESSION['gebruikers_klasid'] == $record['klas_id']) { echo 'checked'; }
echo ' name="groep" value="' . $record['klasNaam'] . '">' . $record['klasNaam'] . '</div>';
}
?>
this should work
<?php
$result = mysqli_query($db,"SELECT * FROM klassen");
while($record = mysqli_fetch_array($result)) {
if($_SESSION['gebruikers_klasid'] == $record['klas_id']) {
$var = 'checked';
}
echo '<div class="groepitem"><input type="radio" '.$var.' name="groep" value="' . $record['klasNaam'] . '">' . $record['klasNaam'] . '</div>';
}
?>
EDIT: now session check it's inside the loop
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?
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"
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>";
}