change order of list that uses pagination - php

I have a paginated list that displays 10 items to a page, but I want to give the option for someone to change the order of the list, for example, by gender or age.
The pagination works, and in terms of sorting the list, so far I have:
$orderby = ($_GET['orderby'] == 'gender')? $_GET['orderby'] : 'age';
if ($result = $mysqli->query("SELECT * FROM people ORDER BY $orderby"))
With 2 basic links: gender | age
echo "<a href='{$_SERVER['PHP_SELF']}?orderby=gender'>gender</a> | <a href='{$_SERVER['PHP_SELF']}?orderby=age'>age</a>";
When I click one of the links, it changes the order of the list as required, but when I go to another page of the pagination, it does not work.
Can someone please let me know of the best way to fix this. Thanks.

Try my function genPagnation(), please refer my code below:
<?
$totalPage = 23;
$currentPage = isset($_GET['p']) && is_numeric($_GET['p']) && $_GET['p'] >= 1 ? (int)$_GET['p'] : 1;
$params = array();
$devices = array();
if( isset($_POST['order']) || isset($_GET['order']) ){
$params['order'] = isset($_POST['order']) ? $_POST['order'] : $_GET['order'];
}
if( isset($_POST['device']) || isset($_GET['device']) ){
$params['device'] = isset($_POST['device']) ? $_POST['device'] : $_GET['device'];
$devices = $params['device'];
}
for( $i = 1; $i <= $totalPage; $i++ ){
if( $i == $currentPage )
print(" <b><u>{$i}</u></b> ");
else
print(" <a href='" . genPagnation($i, $params) . "'>{$i}</a> ");
}
print("<p><hr /></p>\n\n");
function genPagnation($p, $param = array(), $pname = null, $pvalue = null){
$param['p'] = $p;
if( !empty($pname) && !empty($pvalue) )
$param[ $pname ] = $pvalue;
$query = http_build_query($param);
$query = preg_replace('/%5B[0-9]+%5D/', '%5B%5D', $query);
return '?' . $query;
}
?>
<p>
Order By:
<a href='<?=genPagnation($currentPage, $params, "order", "male")?>'>Male</a> |
<a href='<?=genPagnation($currentPage, $params, "order", "female")?>'>Female</a>
</p>
<form method="POST" action="<?=$_SERVER['PHP_SELF']?>">
<p>
Filtering:
<label><input type="checkbox" name="device[]" value="iphone" <? if( !empty($devices) && in_array('iphone', $devices) ){ echo 'checked="checked"'; } ?> />iPhone</label>
<label><input type="checkbox" name="device[]" value="ipad" <? if( !empty($devices) && in_array('ipad', $devices) ){ echo 'checked="checked"'; } ?> />iPad</label>
<label><input type="checkbox" name="device[]" value="ipod" <? if( !empty($devices) && in_array('ipod', $devices) ){ echo 'checked="checked"'; } ?> />iPod</label>
<label><input type="checkbox" name="device[]" value="android" <? if( !empty($devices) && in_array('android', $devices) ){ echo 'checked="checked"'; } ?> />Android</label>
</p>
<p>
Ordering:
<label><input type="radio" name="order" value="male" <? if( isset($_REQUEST['order']) && $_REQUEST['order'] == 'male'){ echo 'checked="checked"'; } ?> />Male</label>
<label><input type="radio" name="order" value="female" <? if( isset($_REQUEST['order']) && $_REQUEST['order'] == 'female'){ echo 'checked="checked"'; } ?> />Female</label>
</p>
<p><input type="submit" value="search"></p>
</form>

Related

How to show a php radio input value on a php email?

I have a php form with 4 radio inputs as below:
<?php if( $chauffeur_data['enable-paypal'] == '1' ) { ?>
<div class="radio-wrapper clearfix"><input type="radio" name="payment-method" value="paypal" <?php if( $paypal_check == '1' ) { echo 'checked="checked"'; } ?> /><label><?php esc_html_e('Pay with PayPal','chauffeur'); ?></label><img src="<?php echo plugins_url('../../assets/images/paypal.png', __FILE__); ?>"></div>
<?php } ?>
<?php if( $chauffeur_data['enable-stripe'] == '1' ) { ?>
<div class="radio-wrapper clearfix"><input type="radio" name="payment-method" value="stripe" <?php if( $stripe_check == '1' ) { echo 'checked="checked"'; } ?> /><label><?php esc_html_e('Pay with Credit Card','chauffeur'); ?></label><img src="<?php echo plugins_url('../../assets/images/stripe.png', __FILE__); ?>"></div>
<?php } ?>
<?php if( $chauffeur_data['enable-cash'] == '1' ) { ?>
<div class="radio-wrapper clearfix"><input type="radio" name="payment-method" value="cash" <?php if( $cash_check == '1' ) { echo 'checked="checked"'; } ?> /><label><?php esc_html_e('Pay with Cash to the Driver','chauffeur'); ?></label></div>
<?php } ?>
<?php if( $chauffeur_data['enable-pos'] == '1' ) { ?>
<div class="radio-wrapper clearfix"><input type="radio" name="payment-method" value="pos" <?php if( $pos_check == '1' ) { echo 'checked="checked"'; } ?> /><label><?php esc_html_e('Pay with Card on POS in the Car','chauffeur'); ?></label></div>
<?php } ?>
<?php // If all payment gateways are disabled
if( $chauffeur_data['enable-paypal'] != '1' && $chauffeur_data['enable-stripe'] != '1' && $chauffeur_data['enable-cash'] != '1' &&
$chauffeur_data['enable-pos'] != '1' ) { ?>
<input type="hidden" name="payment-method" value="cash" />
<?php } ?>
<button name="pay_now" id="pay_now" class="payment-button" type="submit">
<?php esc_html_e('Proceed To Payment','chauffeur'); ?>
</button>
<?php } else { ?>
<input type="hidden" name="payment-method" value="cash" />
<button name="pay_now" id="pay_now" class="payment-button" type="submit">
<?php esc_html_e('Proceed To Book','chauffeur'); ?>
</button>
<?php } ?>
When one of the radio buttons is checked, how can I show its value (e.g. pos) to the email I send?
The email is like below. What should be my code to show the radio button choice?
$customer_email_content .= '<li><strong>' . esc_html__('Payment Method you Selected','chauffeur') . ': </strong>' . '**payment-method-goes-here**' . '</li>'."\r\n";
After form is submitted, get the radio button value from $_POST array;
$payment_method = $_POST['payment-method'];
Use $payment_method to show the value of the radio button that is selected.
$customer_email_content .= '<li><strong>' . esc_html__('Payment Method you Selected','chauffeur') . ': </strong>' . $payment_method . '</li>'."\r\n";

Loop form in PHP, Submit all forms with 1 button

I have a question. I want to make a history timeline, on this timeline the user can add 'elements' (e.g. a element for each year.).
In form 1 the user creates timeline and says the amount of elements on the timeline this gets send to the database (this works).
In form 2 the user can fill the elements. (This doesnt work).
In form 1 (if someone wants 5 elements) after posting, the database creates 5 records (for each element one).
Now I want 5 different forms that fills these 5 elements (Title, description etc.), but with 1 submit button.
Now if i try this (see code below), only the 5th form gets posted...
How do I get this to work?
Thanks in advance,
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once('classes/database.class.php');
$DB = Database::getInstance();
$titel = '';
$beschrijving = '';
$afbeeldingURL = '';
$jaar = '';
$element_id = '';
$velden = '';
$sql3 = "SELECT `id` , `aantal_elementen` FROM `tijdlijn` ORDER BY id DESC LIMIT 0, 1";
$last_id = $DB->_query($sql3);
if ($last_id->num_rows > 0) {
while($row = $last_id->fetch_assoc()) {
$last_id2 = $row["id"];
$aantal_elementen3 = $row["aantal_elementen"];
}
}
$sql4 = "SELECT * FROM `elementen` WHERE `tijdlijn_id` = ".$last_id2."";
$sql5 = "SELECT `id` FROM `elementen` WHERE `tijdlijn_id` = ".$last_id2." ORDER BY id ASC LIMIT 0,1";
$sql6 = "SELECT `id` FROM `elementen` WHERE `tijdlijn_id` = ".$last_id2." ORDER BY id DESC LIMIT 0,1";
$sql7 = "SELECT `id` FROM `elementen` WHERE `tijdlijn_id` = ".$last_id2." ORDER BY id ASC";
$last_id3 = $DB->_query($sql4);
$min_id = $DB->_query($sql5);
$min_id = mysqli_fetch_assoc($min_id);
$min_id = $min_id['id'];
$max_id = $DB->_query($sql6);
$max_id = mysqli_fetch_assoc($max_id);
$max_id = $max_id['id'];
$dingid[] = $DB->_query($sql7);
echo $min_id."</br>";
echo $max_id."</br>";
$current_id = $min_id;
if (isset($_POST) && !empty($_POST)) {
$boolError = false;
foreach ($_POST as $k => $v) {
$_POST[$k] = trim($v);
}
if (!isset($_POST['titel']) || trim($_POST['titel']) == '') {
$titel = 'error';
$boolError = true;
}
if (!isset($_POST['beschrijving']) || trim($_POST['beschrijving']) == '') {
$beschrijving = 'error';
$boolError = true;
}
if (!isset($_POST['afbeeldingURL']) || trim($_POST['afbeeldingURL']) == '') {
}
if (!isset($_POST['jaar']) || trim($_POST['jaar']) == '') {
$jaar_start = 'error';
$boolError = true;
}
if (
!isset($_POST['element_id'])
|| trim($_POST['element_id']) == ''
|| !preg_match('/^\d+$/', $_POST['element_id'])
) {
$element_id = 'error';
$boolError = true;
}
if ($boolError === false) {
$sql = "UPDATE `elementen`
SET
`titel` = '" . $_POST['titel'] . "',
`beschrijving` = '" . $_POST['beschrijving'] . "',
`afbeelding_url` = '" . $_POST['afbeeldingURL'] . "',
`jaar` = '" . $_POST['jaar'] . "'
WHERE `id` = ".$_POST['element_id'].";
";
if ($DB->_query($sql)) {
//$current_id++;
//exit;
} else {
header('Location: ?oops2');
exit;
}
} else {
$velden = "Ff alles invullen he";
}
?> <p>Vul het formulier hier onder in voor de elementen en maak een tijdlijn!</p><?php
}
if ($last_id3->num_rows > 0) {
while($row2 = $last_id3->fetch_assoc()) {
$last_id4 = $row2["id"];
?>
<form class="form-nieuws" method="post">
</br></br>
<?php echo $last_id2; ?>
<?php echo $current_id; ?>
<input id="titel2" class="<?= $titel ?> form-control" type="text" placeholder="Titel tijdlijn" name="titel" value="<?= isset($_POST['titel']) ? $_POST['titel'] : '' ?>">
<br>
<textarea id="beschrijving2" class="<?= $beschrijving ?> form-control" rows="3" placeholder="Beschrijving over tijdlijn" name="beschrijving" value="<?= isset($_POST['beschrijving']) ? $_POST['beschrijving'] : '' ?>"></textarea>
<br>
<input id="afbeeldingURL2" class="<?= $afbeeldingURL ?> form-control" type="text" placeholder="URL voor afbeelding" name="afbeeldingURL" value="<?= isset($_POST['afbeeldingURL']) ? $_POST['afbeeldingURL'] : '' ?>">
<br>
<input id="jaar" class="<?= $jaar ?> form-control" type="text" placeholder="Start jaar tijdlijn" name="jaar" value="<?= isset($_POST['jaar']) ? $_POST['jaar'] : '' ?>">
<input id="element_id" class="<?= $element_id ?> form-control" type="text" placeholder="<?php echo $last_id4 ?>" name="element_id" value="<?= isset($_POST['element_id']) ? $_POST['element_id'] : '' ?>">
<div class="foutlabel">
<em class="<?= ($velden) ? 'error' : '' ?>"><?= ($velden) ? $velden : '' ?>
</em>
</div>
</br>
<?php $current_id++;}
} ?> <div class="gabutton">
<input type="submit" value="Creeer tijdlijn">
</div>
</form>
The best way is to have only one form with input for each elements. You generate each form input in function of your elements.
With this way, you only have one button and one call to send data to your server.
In server side, you just need to parse and check data and be carefull about number of element you send.

Checking radio button on submit - PHP

I need to check a radio button input on submit.
If none of the radio buttons are checked, $err1_diet gets set to true and the red class needs to be added.
And I also need to know which radio button was checked since this isn't the only question in the form.
if( !isset($_POST['diet']) ){
$err1_diet = true;
}elseif($_POST['diet'] == 1){
$diet = true;
}else{
$diet = false;
$yes = true;
}
<p class="<?php echo (($err1_diet == true) ? "red" : "" ); ?>">• Are you on a diet?<?php var_dump($err1_diet); ?></p>
<input type="radio" name="diet" value="1" <?php echo (($diet) ? 'checked="true"' : "" ); ?> /> Yes
<input type="radio" name="diet" value="0" <?php echo (($diet) ? '' : 'checked="true"' ); ?> /> No
Actually no ... it's checking one of the radio buttons before the form is submitted. That's the problem
You can solve your problem using $_SERVER['REQUEST_METHOD']=='POST' for example:
if( $_SERVER['REQUEST_METHOD']=='POST' and !isset($_POST['diet']) ){
$err1_diet = true;
}elseif($_POST['diet'] == 1){
$diet = true;
}else{
$diet = false;
$yes = true;
}
<p class="<?php echo (($err1_diet == true) ? "red" : "" ); ?>">• Are you on a diet?<?php var_dump($err1_diet); ?></p>
<input type="radio" name="diet" value="1" <?php echo (($diet) ? 'checked="true"' : "" ); ?> /> Yes
<input type="radio" name="diet" value="0" <?php echo (($diet) ? '' : 'checked="true"' ); ?> /> No

Populate Checkbox from Database Query

I'm trying to check a check box, if the value for that field is 1 in the database.
I have:
<?php
$selectedSPK=$_POST['SPKSelect'];
$assigned = $_POST['Sales_Exec'];
$date = $_POST['DateSelect'];
if ($selectedSPK) {
$Priorityquery = "SELECT Priority FROM Data WHERE SPKCustNo = '$selectedSPK' ";
$Priorityresult = mysql_query($Priorityquery);
$row = mysql_fetch_array($Priorityresult);
$checked = $Priorityresult['Priority'];
}
?>
<input name="PriorityCheckBox" type="checkbox" value="1"
<?php if ($checked == 1) echo ' checked'; ?> />
but not getting any joy, any ideas?
Try this:
You were not using the row returned by the query...
<?php
$selectedSPK=$_POST['SPKSelect'];
$assigned = $_POST['Sales_Exec'];
$date = $_POST['DateSelect'];
if ($selectedSPK)
{
$Priorityquery = "SELECT Priority FROM Data WHERE SPKCustNo = '$selectedSPK' ";
$Priorityresult = mysql_query($Priorityquery);
$row = mysql_fetch_array($Priorityresult);
//$checked = $Priorityresult['Priority']; // <------ this is where you went wrong...
$checked = $row['Priority']; // <------ this will fix where u went wrong!
}
?>
<input name="PriorityCheckBox" type="checkbox" value="1" <?php if ($checked == 1){echo ' checked'; }?>
You should use
<?php if ($checked == 1){echo "checked='checked'"; }
and also
$checked = $Priorityresult['Priority'];
to
$checked = $row['Priority'];
I think you have one mistake... Try this
<input name="PriorityCheckBox" type="checkbox" value="1" <?php if ($row['Priority'] == 1) echo ' checked'; ?> />
Try it this way
<input name="PriorityCheckBox" type="checkbox" value="1" <?php if ($checked == 1) echo "checked='checked'"; ?> />
change
<?php if ($checked == 1) echo ' checked'; ?>
to
<?php if ($checked == 1) echo ' checked="checked"'; ?>
and $checked = $Priorityresult['Priority']; to $checked = $row['Priority'];
It should be checked="checked"
<?php if ($checked == 1) echo "checked='checked'"; ?>

Update database with checkbox php mysql

I want to update the database with a checkbox in checked state.
If it is checked then update the database with 1.
Else if it is unchecked then update it with 0.
It works fine but it doesn't work if unchecked.
<?php
include('lib/db.php');
$facebook_id ="10001088";
$query1 = "SELECT `video`,`quran`,`medical`,`groups` FROM `man_facebook`.`users` WHERE `facebook_id`='$facebook_id'";
$result1 = mysql_query($query1);
while($result = mysql_fetch_array($result1))
{
$video = $result['video'];
$quran = $result['quran'];
$medical = $result['medical'];
$groups = $result['groups'];
echo $video;
// echo $quran;
?>
<form method="post" action="<? echo $_SERVER['REQUEST_URI']; ?>" >
<input type="checkbox" name="video" id="video" value="<?echo $video;?>" <?php
if($video == '1'){
echo "checked='checked'";
}
else {}
echo "/>"
?>
<input type="submit" name="submit" value="Submit">
</form>
<?php
}
if (isset($_POST['submit']))
{
if (is_numeric($_POST['video']) && $_POST['video'] <2 )
{
$video1 = isset($_POST['video']) ? '1' : '0';
echo $video1;
$query = mysql_query("UPDATE `man_facebook`.`users`
SET `video` ='$video1'
WHERE `facebook_id`='$facebook_id'");
$video = $video1;
echo '<meta http-equiv="refresh" content="0" />';
}
}
//echo $query;
//header("Location: updatesql.php");
?>
Can I also use jquery to update it smoothly?
I find this part a bit weird:
if (is_numeric($_POST['video']) && $_POST['video'] <2 )
{
$video1 = isset($_POST['video']) ? '1' : '0';
You first check if it is numeric and then check if it is set. If it wasn't set, that if would become false automatically. In other words, $video is always 1. Assuming $video can be only true/false (or maybe it comes as "checked"/"unchecked", not really sure), use it like this:
$video1 = ($video ? '1' : '0');
Hopefully I spotted the issue successfully :)
UPDATE
<input type="checkbox" name="video" id="video" value="video" <?php
if($video == '1'){
echo "checked='checked'";
}
echo "/>";
...
if (isset($_POST['submit']))
{
echo $_POST['video']; // again, please tell what it outputs here!!!
$video1 = (($_POST['video'] == "video") ? '1' : '0');
checkbox update
if (isset($_POST["send"]) && $_POST["send"] == "ok") {
foreach ($_POST['membre'] as $id => $data) {
mysql_query("UPDATE tablename SET param1='" . $data['param'] . "' WHERE id='" . $id . "'");
}
}
<form>
<input type="hidden" name="send" value="ok" />
<td class="cel2" style=" width: 100px;">
<input name="membre[<?php print $val['id']; ?>][param]" type="checkbox" style="margin-top: 7px; width: 30px;" value="1" id="param" <?php if ($val['p'] == '1') print 'checked'; ?> />
</td>
</form>

Categories