PHP combined with HTML code - php

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

Related

PHP Why Else statment strangely doesnt work

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 ?

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?

jquery simplecart php variables

I'm having difficulties with the onclick jquery within my php code, how do I get a php variable so that name=variable and price=variable?
<?php
$result = mysqli_query($db,"SELECT * FROM table");
while($row = mysqli_fetch_array($result)) {
echo '<ul>';
echo '<li>' . $row['selection'] . '<div style="float:right;"><a href="#" onclick="simpleCart.add('name='. $row['selection'] . ','price='. $row['price'] .');return false;
"> '. $row['price'] . '</a></div></li>';
echo '</ul>';
}
?>
How do I get around this? Thanks
change your code with this line...
echo '<div style="float:right;"><a href="#"
onclick="simpleCart.add(name='.$row['selection'].',price='.$row['price'].')"
</a></div>';
Not faniliar with simplecart, but You definitely need to crrectly escape your quotes.
<?php
$result = mysqli_query($db, "SELECT * FROM table");
while($row = mysqli_fetch_array($result)) {
echo '<ul>';
echo '<li>' . $row['selection'] . '<div style="float:right;"> '. $row['price'] . '</div></li>'; echo '</ul>'; }
?>

Update a variable on click

basically I've written the following but I'm wondering what the best way to get $message_id = to the $row['id'] you click on to view an inbox message. If I just statically set $message_id = to say 39 and click on the row that's $row['id'] is 39 it'll open the message.
Basically what it does is list inbox messages I have, and anchor tag them with its row id to be used in the header to show the full contents of that message.
<p>
<?php
$message_id = "39";
if (isset($_GET[$message_id]) && empty($_GET[$message_id])) {
$username = $user_data['username'];
$get_message = mysql_query("SELECT * FROM `users_inbox` WHERE `id` = '$message_id' AND `send_to` = '$username'");
$get_message_result = mysql_fetch_array($get_message);
echo '<h1><u>' . $get_message_result['subject'] . '</u></h1>';
echo $get_message_result['sent_from'] . '<br />';
echo $get_message_result['body'] . '<br />';
echo 'Back';
}
$username = $user_data['username'];
$result = mysql_query("SELECT * FROM `users_inbox` WHERE `send_to` = '$username'");
?>
<table border="1">
<tr>
<td>From</td>
<td>Subject</td>
<td>Date</td>
</tr>
<?php
while($row = mysql_fetch_array($result))
{
echo
'<tr>' .
'<td>' . $row['sent_from'] . '</td>' .
'<td>' . $row['subject'] . '</td>' .
'<td>' . $row['post_date'] . '</td>' .
'</tr>';
}
?>
</table>
</p>
Anyone know if this is even possible, as I know code is read from top to bottom and as far as I'm aware you cant go back upwards at all.
I assume that the $user_data['username'] is set or available,if yes then you could pass a variable name of you choice which points the message id instead of sending only the id (eg: i am setting it as msg_id ), Now you can access the message id via $_GET['msg_id']
<p>
<?php
$username = $user_data['username'];
if ( isset( $_GET['msg_id'] ) ) {
$get_message = mysql_query("SELECT * FROM `users_inbox` WHERE `id` = '$message_id' AND `send_to` = '$username'");
$get_message_result = mysql_fetch_array($get_message);
echo '<h1><u>' . $get_message_result['subject'] . '</u></h1>';
echo $get_message_result['sent_from'] . '<br />';
echo $get_message_result['body'] . '<br />';
echo 'Back';
}else{
$result = mysql_query("SELECT * FROM `users_inbox` WHERE `send_to` = '$username'");
?>
<table border="1">
<tr>
<td>From</td>
<td>Subject</td>
<td>Date</td>
</tr>
<?php
while($row = mysql_fetch_array($result))
{
echo
'<tr>' .
'<td>' . $row['sent_from'] . '</td>' .
'<td>' . $row['subject'] . '</td>' .
'<td>' . $row['post_date'] . '</td>' .
'</tr>';
}
?>
</table>
</p>

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