Multiple php isset in one page - php

I'm trying to achieve multiple pagination in one page.
The pagination for all the results, is working.
But except from the main results, i want to include different options.
For example, in the image below i have three more options,
Newest, Reputation, Oldest
So, i want multiple pagination for all these options.
In the page that i want to display the results, i have multiple isset(), in order to achieve that.
<div id='paginationUsers'>
<?php
//for all the results that displaying first.
if(isset($_GET['page'])) {
$page = $_GET['page'];
} else {
$page = 1;
}
echo "<input type='hidden' id='pageAll' value='".$page."'>";
// for the Newest results
if(isset($_GET['pageNewest'])) {
$pageNewest = $_GET['pageNewest'];
} else {
$pageNewest = 1;
}
echo "<input type='hidden' id='newPage' value='".$pageNewest."'>";
//for the Oldest results
if(isset($_GET['pageOldest'])) {
$pageOldest = $_GET['pageOldest'];
} else {
$pageOldest = 1;
}
echo "<input type='hidden' id='oldPage' value='".$pageOldest."'>";
//for the Reputation results
if(isset($_GET['pageReputation'])) {
$pageReputation = $_GET['pageReputation'];
} else {
$pageReputation = 1;
}
echo "<input type='hidden' id='reputationPage' value='".$pageReputation."'>";
?>
</div>
Ofcourse, in that way cannot work as you can see in the code.
So, is it possible to include multiple isset in one page?

Try this code:
<?php
function getGET($name) {
if (isset($_GET[$name]) {
return $_GET[$name];
} else {
return 1;
}
}
function genInput($id, $name) {
return "<input type='hidden' id='". $id ."' value='". getGET($name) ."'>";
}
echo "<input type='hidden' id='pageAll' value='". getGet('page')."'>";
echo genInput('newPag', 'pageNewest');
echo genInput('oldPage', 'pageOldest');
echo genInput('', 'pageReputation');

Related

how to return php variable to Jquery function to be used to multiply values

Trying to have several checkboxes multiply and add where there are multiple products. would it be possible to use a for statement in jquery against the number of rows in a database or have it run without it?
I've reduced the code i had to a compact version to add up checkboxes and multiply against a multiplier that is a POST from a previous page.
now this script won't run for any of the products and wandering what could be used to bridge the problem between client side and server side script.
I'm confused about the method needed to use a php variable in a jquery function. I thought of use an onclick to call function for a checkbox clicked but im trying to run this on DOM ready. If anyone can perhaps tell me in the simplest terms how this may be possible I will happily do more research.
<?php
//CONNECTION, QUERY AND MISC ///
$count = mysqli_num_rows($result);
$i = 1;
while ($row = mysqli_fetch_assoc($result))
{
$kilo1 = $row['kilo1'];
$kilo2 = $row['kilo2'];
$price1 = $row['price1'];
$price2 = $row['price2'];
$price3 = $row['price3'];
$price4 = $row['price4'];
$multiplier = 2;
$qri = "qr"."$i";
$userdaily = "userdaily"."$i";
$usertotal = "usertotal"."$i";
$pricef1 = "price1"."$i";
$pricef2 = "price2"."$i";
echo "<form enctype='multipart/form-data' name='contactform' method='POST' action='something.php'>";
echo "<input type='hidden' name='multiplier' id='multiplier' value='$multiplier'>";
echo "<input type='checkbox' name='checkbox1' id='$qri' value='$price1'>";
echo "<input type='checkbox' name='checkbox2' id='$qri' value='$price2'>";
echo "<input type='checkbox' name='checkbox3' id='$qri' value='$price3'
class='insure'>";
echo "<input type='checkbox' name='checkbox4' id='$qri' value='$price4'
class='insure'>";
echo "<input type='checkbox' name='checkbox5' id='$qri' value='$kilo1'
class='kilo'>";
echo "<input type='checkbox' name='checkbox6' id='$qri' value='$kilo2'
class='kilo'>";
/////TOTALS VISIBLE AND HIDDEN/////
echo "<span id='$userdaily'></span>";
echo "<span id='$usertotal'></span>";
echo "<input type='hidden' name='pricef1' id='$pricef1' value=''>";
echo "<input type='hidden' name='pricef2' id='$pricef2' value=''>";
echo "<input type='submit'>";
if($i%4==0 || $i == $count) echo '<br>';
$i++;
}
?>
<script>
$(function()
{
$("input.kilo").change(function()
{
$("input.kilo").not(this).prop("checked", false);
});
$("input.insure").change(function()
{
$("input.insure").not(this).prop("checked", false);
});
$(document).ready(function()
{
//get the values of the selected options
var counter = parseInt("<?php echo $count;?>");
for (i = 1; i <= counter; i++)
{
let v = $.map($("input[id^='qr+i+']:checked"), function(t)
{
return parseFloat($(t).val());
});
let s = v.reduce((a, b) => a + b);
//sum them up to a daily total
console.log(v);
$("#<?php echo $userdaily;?>").text('R' + s + '/day');
$("#<?php echo $usertotal;?>").text('R' + s *
parseFloat($("#multiplier").val()) + '/day');
$("#<?php echo $pricef1;?>").val(s);
$("#<?php echo $pricef2;?>").val(s * parseFloat($("#multiplier").val()));
}
});
});
</script>
I have a list of product each with a daily total and a total of the number of days, each product has completely different prices. I tried to create this in a fiddle and it works find without the use of variables, I realise that Jquery and php are different in their uses and I'm not sure how to work around this.
As treyBake mentionned, if you need to pass information from PHP to jQuery back to PHP and so forth, use hidden DOM elements and make Ajax requests.
This will also allow you to update your data in real time.
Okay so for instance you can hide your number count in a hidden input
<input type="hidden" value="<?php echo $count;?> " name="countrows" id="countrows">
Now you can retrieve in your jQuery :
var counter = $("#countrows").val();
Get value of checked checkbox with is.checked :
var x = $("#checkbox").is(":checked");

Update from checkbox to wrong ID in php

issue for days now.
I have a form with multiple checkboxes, I will never know how many rows will be in the form, and I am aware that an empty checkbox will not post to the processing page. I have also tried providing a hidden value to the checkbox to no avail...
I know there is a straightforward method to this, please assist:
The following code is on the form:
echo "<td><center><input type='checkbox' checked='checked' name='jce_pnt_id[]' value='$jce_pnt_id' onclick='return false;' /><br>$jce_pnt_id</center></td>";
echo "<td title='Task Complete ?'><center><input type='hidden' name='tr_g[]' value='25'>";
if($jce_ins_complete < '100' || $jce_ins_act_complete == '0')
{
echo "<input type='checkbox' name='tr_g[]' id='$jce_pnt_id' value='100'>";
}
else
{
echo "<input type='checkbox' name='tr_g[]' id='$jce_pnt_id' value='100' checked='checked'>";
}
echo "</center></td>";
Here is a screenshot (look at the red circled column):
Here is the processing snippet (not working):
$jce_pnt_id = $_POST['jce_pnt_id'];
$tr_g = $_POST['tr_g']; // jce_pnt_act_complete
foreach ($tr_g as $id => $trg) {
//if(empty($trg)) { $trg = "0"; }
$queryg = "UPDATE `jce_pnt` SET `jce_pnt_act_complete` = '$trg' WHERE `jce_pnt_id` = '$jce_pnt_id[$id]'";
$resultg = mysqli_query($cxn,$queryg);
}
* The value writes to the wrong ID in the database.

How do I use $_SESSION to store items in an array and trigger a button swap?

so I'm trying to store shopping cart items by there ID. Once this happens a button needs to appear "Remove from Cart", if I click this button a new button will appear "Add to Cart" but I'm a little bit lost on it from this point. Im new on here so please excuse my mistakes. My code is below:
session_start();
$items[] = $_POST['add'];
if (isset($_POST['add'])) {
$_SESSION['cart'][]=$_POST['add'];
} else if (isset($_POST['remove'])) {
unset ($_SESSION['cart'][$_POST['remove']]);
}
foreach($vend as $vendID=> $items) {
echo "<form action='vend.php' method='post'>";
echo "<article id ='vend-$vendID'>";
echo "<h1 class = 'item-h1' id = 'h1'>{$items['title']}</h1>";
echo "<div class ='item-no'>";
echo "<p class = 'pro-id'><b>Product ID: </b>{$vendID}</p></div>";
echo "</div>";
echo "<div class ='img-div'>";
echo "<img src=../images/{$items['img']} alt='' height='196' width='200'></div>";
echo "<div class='item-p'>";
echo "<p>{$items['desc']}</p></div>";
echo "<div class='pricing'>";
echo "<p><b>Price: $</b>{$items['price']}</p></div>";
//echo "<button name='add' type='submit' value='$vendID'>Add to Cart</button>";
if(isset($_POST['add']) && ($_SESSION['cart'] == $vendID)) {
echo "<button name='remove' type='submit' value='$vendID'>Remove from Cart</button>";
}
else {
echo "<button name='add' type='submit' value='$vendID'>Add to Cart</button>";
}
In your case here what should be done:
session_start();
/* Check if $_SESSION['cart'] exists */
if (!isset($_SESSION['cart'])) {
/* Init cart as empty array */
$_SESSION['cart'] = [];
}
if (isset($_POST['add']) && 0 < $_POST['add']) {
/* Add product id to session cart */
$_SESSION['cart'][$_POST['add']] = 1;
} else if (isset($_POST['remove']) && 0 < $_POST['remove']) {
/* Remove product id from session cart */
unset($_SESSION['cart'][$_POST['remove']]);
}
// I check with `0 < $id` because ids are always positive
foreach($vend as $vendID=> $items) {
echo "<form action='vend.php' method='post'>";
echo "<article id ='vend-$vendID'>";
echo "<h1 class = 'item-h1' id = 'h1'>{$items['title']}</h1>";
echo "<div class ='item-no'>";
echo "<p class = 'pro-id'><b>Product ID: </b>{$vendID}</p></div>";
echo "</div>";
echo "<div class ='img-div'>";
echo "<img src=../images/{$items['img']} alt='' height='196' width='200'></div>";
echo "<div class='item-p'>";
echo "<p>{$items['desc']}</p></div>";
echo "<div class='pricing'>";
echo "<p><b>Price: $</b>{$items['price']}</p></div>";
if(isset($_SESSION['cart'][$vendID])) {
// you have `$vendID` in session cart - then show Remove button
echo "<button name='remove' type='submit' value='$vendID'>Remove from Cart</button>";
} else {
// you DON'T have `$vendID` in session cart - then show Add button
echo "<button name='add' type='submit' value='$vendID'>Add to Cart</button>";
}
// Also don't forget to close `</form>`
}
To remove an item from your cart you are accessing the item based on your $vendID variable. Which would require your item to be stored in $_SESSION['cart'] as an array:
$_SESSION['cart'][0] = "item 1";
$_SESSION['cart'][1] = "item 2";
...
but later in your code you are accessing $_SESSION['cart'] as if it is just a value not an array
adding an item in your code is done with the following line:
$_SESSION['cart'][]=$_POST['add'];
This results in something like this:
//empty cart
$_SESSION['cart'][0] = $vendid;
Turns out I needed a underscore , i had $POST instead of $_POST and it works now, thanks all :)

Using a generic if statement for all values of an array in PHP

I have this php statement that collects data from a database table. It loops over all rows and separates them into tables of 2 (home team and away team for that game). I also have radio buttons so the user can pick which team they would like to win for each game. There are 9 games.
$games = array();
for($num = 1; $num <= 9; $num++)
{
$games[$num] = "<table border='1'><tr><b><h4>Game ".$num."</h4></b><th>Home</th><th>Draw</th><th>Away</th></tr>";
}
while ($row = mysql_fetch_array($rs))
{
for($num = 1; $num <= 9; $num++)
{
$games[$num] .=
" <tr>
<td><input type='radio' id='home".$num."' name='game".$num."' value='".$row['home'.$num]."' > ".$row['home'.$num]."</td>
<td>Draw <br /><input type='radio' id='draw".$num."' name='game".$num."' value='0'></td>
<td>".$row['away'.$num]."<input type='radio' id='away".$num."' name='game".$num."' value='".$row['away'.$num]."'></td>
</tr>";
}
}
On submit, i want to be able to do something for each game via SELFSUBMIT.
So if a user selects home team for game n, i want to display a message.
if($_SERVER["REQUEST_METHOD"] == "POST")
{
if($_POST['game".$num."'] = ('home'.$num))
{
echo ('home'.$num);
}
}
Thats my wrong php code. Please tell me if i can workaround this.
You might want to use jquery
$(document).ready( function(){
$("input[id^=home]").on("click", function(){
$("#spanDisplay").html( $(this).attr("id") );
//you can also display $(this).attr("name") or $(this).val() to print the value
});
});
In your html, you can use span or div to display the value of id for example.
So in your span
<span id="spanDisplay"></span>
UPDATE: using PHP only
$games[$num] .= "<form name="aForm" method="POST" action="afterSubmit.php">";
for($num = 1; $num <= 9; $num++){
$games[$num] .= "<tr><td><input type='radio' id='home".$num."' name='game".$num."' value='".$row['home'.$num]."'> ".$row['home'.$num]."</td>
<td>Draw <br /><input type='radio' id='draw".$num."' name='game".$num."' value='0'></td>
<td>".$row['away'.$num]."<input type='radio' id='away".$num."' name='game".$num."' value='".$row['away'.$num]."'></td></tr>";
}
$games[$num] .= "</form>";
You will need to surround your input with the form tag, submit and post it to the next page for instance afterSubmit.php. So in afterSubmit.php page, you can put something like this:
for($num = 1; $num <= 9; $num++){
if( isset( $_POST['game".$num."'] ) ){
echo $_POST['game".$num."']; //echo the value
}
}

How get post value in pagination

I am working on search functionality with pagination in php.Below is my code but whenever I click on next link the search query is not taking variable which is passed through POST.
Can you please help me..
searchstr=$_POST['q'];
session_start();
$_SESSION['searchchar']=$searchstr;
$rec_limit=3;
$connection=connection();
$searchstr=$_REQUEST['q'];
$sql="Select count(*) from FIMTRN_Memorial where FirstName like '%".$_SESSION['searchchar']."%'";
echo$sql;
$result1=mysql_query($sql);
if(! $result1)
{
die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($result1,MYSQL_NUM );
$rec_count = $row[0];
echo$rec_count;
if( isset($_GET{'page'} ) )
{
$page = $_GET{'page'} + 1;
$offset = $rec_limit * $page ;
}
else
{
$page = 0;
$offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);
$sql="Select * from FIMTRN_Memorial where FirstName like '%".$_SESSION['searchchar']."%' limit $offset,$rec_limit";
echo$sql;
$result=mysql_query($sql);
$rec_num=mysql_num_rows($result);
if($rec_num>0)
{
while($row = mysql_fetch_array($result,MYSQL_ASSOC))
{
$fname=$row['FirstName'];
$image=$row['ProfileImagePath'];
$about=$row['About'];
$url=$row['CustomURL'];
$DOB=$row['DOB'];
$DOD=$row['DOD'];
?>
<div class="search">
<p class="content"><img src="<?php echo $image;?>" style="border:1px solid red;width:100px;height:100px;" /></p>
<div class="content"><p>
<?php
echo "$fname";
?>
<p>(<?php echo date("Y",strtotime($DOB));?>-<?php echo date("Y", strtotime($DOD));?>)</p>
</p><p><?php echo$about?></p>
</div>
</div>
<?php
}
if( $page > 0 & $left_rec > $rec_limit)
{
$last = $page - 2;
echo "Previous Records |";
echo "Next Records";
}
else if( $page == 0 & $rec_count>$rec_limit )
{
echo "Next Records";
}
else if( $left_rec < $rec_limit & $rec_count>$rec_limit )
{
$last = $page - 2;
echo "Previous Records";
}
}
else
{
echo"No memorial found.Please try with other name.";
}
enter code here
?>
There are a couple problems in your code.
First:
You are using the $_GET variable incorrectly. You have $_GET{'page'}, but you should be using it the same way as the$_POST variable, like this: $_GET['page']
Second:
You are interpreting the idea of POSTing and clicking links incorrectly. What you need to make with your next and previous buttons are a hidden form that has all the other variables you need. For example, the simplest way that won't involve changing a lot of your other code:
if( $page > 0 & $left_rec > $rec_limit)
{
$last = $page - 2;
// Previous button form
echo "<form action='?page=$last' method='POST'>";
echo "<input type='hidden' name='q' value='" . $_POST['q'] . "' />"; // This will be a field that saves information but won't appear on the page (though a user could see it if they view source)
echo "<input type='submit' value='Previous Records' />";
echo "</form>";
echo " | "; // The delimiting character you were using, you'll need to style the input type='submit's to look more like a link if that's what you are going for
// Next button form
echo "<form action='?page=$page' method='POST'>";
echo "<input type='hidden' name='q' value='" . $_POST['q'] . "' />"; // This will be a field that saves information but won't appear on the page (though a user could see it if they view source)
echo "<input type='submit' value='Next Records' />";
echo "</form>";
}
This method will keep your variables in the request. Using a link essentially resets the $_POST variable. There are a ton of other possible solutions, but this is one will require the least amount of work to modify your example.
if you are creating sorting and searching functionality than I will suggest you to use $_SESSION variable rather than $_POST or $_GET it will help you manage your grid/data very well.

Categories