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?
Related
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 ?
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
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"
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++;
}
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>";
}