Paginating mysql data using PHP - php

I borrow this code from this video: https://www.youtube.com/watch?v=takddjxhWT0
But when I run this code manually, the data that I get into the database doesn't change when I click the other page number. What do you think is the best code in order to run code this manually?
Any comment will help, thanks.
<html>
<body>
<?php
mysql_connect("localhost","root","");
mysql_select_db("steer");
$page=(isset($_GET["page"]));
if($page=="" || $page=="1")
{
$page1=0;
}
else
{
$page1=($page*12)-5;
}
$res=mysql_query("select * from studtut LIMIT $page1,5");
while($row=mysql_fetch_array($res))
{
echo $row["id"]." ".$row["name"];
echo "<br>";
}
$res1=mysql_query("select * from studtut");
$cou=mysql_num_rows($res1);
$a=$cou/5;
$a=ceil($a);
echo "<br>";
echo "<br>";
for($b=1;$b<=$a;$b++){
?><?php echo $b; " " ?> <?php
}
?>
</body>
</html>

You need to set the page variable. This:
<a href="paging.php?=<?php echo $b; ?>"
Must be changed to this:
<a href="paging.php?page=<?php echo $b; ?>"
Also when you get the variable, you must assign it's value. And not the value, returned by isset function:
$page= isset($_GET["page"]) ? $_GET["page"] : 1;

Use <a href="paging.php?page=<?php echo $b; ?>" .Why ?
Because you access $_GET["page"] as Query string in PHP file . So it should be set in Link so that it doesnt become Blank in PHP file.
In your case $page is always Blank as its not defined . Turn error_reporting(E_ALL) always in Development mode to see errors,warnings,notices.

Related

How to sub a html and php code with php open and close tag into a php echo function

I want to echo out this code but there's already open and close php tag , so how do I sub the html and php in the echo and make the php function works.
This is the code that I want to echo it out.
<div class="tutor-zoom-join-button-wrap">
<?php echo $browser_text; ?>
<?php _e('Join in Zoom App', 'tutor-pro'); ?>
</div>
This is the function that I made(replace with code above with 123)
<?php
$var1 = 1;
if ($var1 = 1) {
echo "123 ";
} else {
echo "The course ID, password and join button will only be shown before 30min of course start";
}
?>
This is what I try but not working
<?php
$var1 = 1;
if ($var1 = 1) {
echo
"<div class="tutor-zoom-join-button-wrap">"
"",.$browser_text."",
""._e('Join in Zoom App', 'tutor-pro')."",
"</div>",
"</div>" ;
} else {
echo "The course ID, password and join button will only be shown before 30min of course start";
}
?>
I have modified your code. Please check and you also refer to some PHP tutorials to learn php.
<?php
$var1 = 1;
if ($var1 == 1) {
echo
"<div class='tutor-zoom-join-button-wrap'>
<a href=" .$browser_url. " target='_blank' class='tutor-btn tutor-button-block'>".$browser_text."</a>,
<a href=".$meeting_data['join_url']."target='_blank' class='tutor-btn bordered-btn tutor-button-block'>"._e('Join in Zoom App', 'tutor-pro')."</a>",
"</div>",
"</div>" ;
}
else {
echo "The course ID, password and join button will only be shown before 30min of course start";
}
?>
The problem is in the long string you want to echo. The double quote " starts and ends the string. You have started and ended the string several times when what you want is one long string. If you want to use a double quotes inside the string you have to precede it with a slash \". To concatenate strings you need to use a dot .. Double quotes also allow you to place variables inside the string that will be replaced. So your echo statement could be done like this:
echo
"<div class=\"tutor-zoom-join-button-wrap\">" .
"$browser_text" .
"" . _e('Join in Zoom App', 'tutor-pro') . "" .
"</div>" .
"</div>" ; // Note that this tag does not appear to be necessary based on the code you have shown
You can write it like this:
<?php
$var1 = 1;
if ($var1 = 1) {
echo `<div class="tutor-zoom-join-button-wrap">
`.$browser_text.`
`._e('Join in Zoom App', 'tutor-pro').`
</div>
</div>` ;
} else {
echo "The course ID, password and join button will only be shown before 30min of course start";
};
?>
Hope It will work for you!

If session has value echo it else echo "all"

Update 2
I'll just leave this here for future references. But this is the solution I created with all the help I got here. Thanks!
<script>
function Refresh() {
location.reload();
}
</script>
<?php $number = $_SESSION["page_id"]; ?>
<?php
if (isset($_SESSION['page_id']) && !empty($_SESSION['page_id'])) {
echo do_shortcode('[RICH_REVIEWS_SHOW category="page" num="all" id="'. $number .'"]');
session_destroy();
echo ('<button class="btn btn-0001" onclick="Refresh()">Show All</button>');
}
else{ echo do_shortcode('[RICH_REVIEWS_SHOW num="all"]'); }
;
?>
Update
So I'm trying to just do a simple echo to see if the session is set using this code:
<?php if(isset($_SESSION['page_id']) && !empty($_SESSION['page_id'])) {
echo 'Set and not empty, and no undefined index error!');
};?>
But doing this breaks my page, I just get a blank page? How do I check if the session is set? When I do a echo of the session using this code:
<?php echo $_SESSION["page_id"]; ?>
It does output the correct session value?? What am I doing wrong?
I have a sessions saved with PHP and I'm using this so that the page ID from Wordpress is echo-ed in a shortcode do_shortcode('');
This is what my code looks like:
<?php $number = $_SESSION["page_id"]; ?>
<?php echo do_shortcode('[RICH_REVIEWS_SHOW category="page" num="all" id="'. $number .'"]'); ?>
<?php echo do_shortcode_all('[RICH_REVIEWS_SHOW category="page" num="all" id="all"]'); ?>
<?php echo $shortcode ;?>
<?php echo $shortcode_all ;?>
Now, what I would like to do is IF the page_id is not stored in the session it should echo all. So how do I go about this?
I found this code, and I think its something I need... But I'm not that great a programmer/coder...
<?php
$var = 0;
// Evaluates to true because $var is empty
if (empty($var)) {
echo '$var is either 0, empty, or not set at all';
}
// Evaluates as true because $var is set
if (isset($var)) {
echo '$var is set even though it is empty';
}
?>
So, if I where to put these two together I would get something like this
<?php
$number = $_SESSION["page_id"];
// Evaluates to true because $var is empty
if (empty($number)) {
echo $shortcode_all ;
}
// Evaluates as true because $var is set
if (isset($number)) {
echo $shortcode ;
}
?>
Am I in the right direction?
Solution
<?php
if (isset($_SESSION['page_id']) && !empty($_SESSION['page_id'])) {
echo('Set and not empty, and no undefined index error!');
};
?>
What am I supposed to feel from the missing (?
If you are using wordpress then you should use the session_start(); in wp-config.php file so please first put in you wp-config.php at top and then check.
<?php
$number = $_SESSION["page_id"];
// Evaluates to true because $var is empty
if (empty($number) || $number=='') {
echo $shortcode_all ;
}else{
echo $shortcode ;
}
?>

How to pass $_GET in pagination in php

This code works perfectly if i am just running the pagination, but when there is a $_GET variable passed it shows the first page properly and when clicked on next it loads all the data in the db and runs from beginning.
I would like to know how can i pass a $_GET variable passed.
<?php
if ($currentpageRec > 1) {
echo " <a href='{$_SERVER['PHP_SELF']}?currentpageRec=1'>First Page</a> ";
prevpageRec = $currentpageRec - 1;
echo " <a href='{$_SERVER['PHP_SELF']}?currentpageRec=$prevpageRec'>Previous Page</a> ";
}
for ($x = ($currentpageRec - $rangeRec); $x < (($currentpageRec + $rangeRec) + 1); $x++) {
if (($x > 0) && ($x <= $totalpagesRec)) {
if ($x == $currentpageRec) {
echo " [<b>$x</b>] ";
} else {
echo " <a href='{$_SERVER['PHP_SELF']}?currentpageRec=$x'>$x</a> ";
}
}
}
if ($currentpageRec != $totalpagesRec) {
$nextpageRec = $currentpageRec + 1;
echo " <a href='{$_SERVER['PHP_SELF']}?currentpageRec=$nextpageRec'>Next Page</a> ";
echo " <a href='{$_SERVER['PHP_SELF']}?currentpageRec=$totalpagesRec'>Last Page</a> ";
}
?>
Based on your code, I am wondering whether you should register global variables
http://php.net/manual/en/security.globals.php
Your code could use the variables as globals, whereas in fact it should refer to the GET variables, e.g.
<?php
if ($_GET['currentpageRec'] > 1) {
//[etc]
?>
Further to the comment below: I tried to say: user $_GET (etcetera), as the code appears to be based on the (depreciated) global variabels.
--- edit ---
Further to the updated shared code: you have to pass each variable again when using the pagination, e.g. for your last line
<?
// code to define $totalpagesRec
// code to define $KeyWord
// personal preference to split the echo between static texts and variables
echo " <a href='".$_SERVER['PHP_SELF']."?currentpageRec=".$totalpagesRec."&keyword=".$KeyWord."'>Last Page</a> ";
?>
Hope this helps

If variable is link, echo link

I having issues getting a function to echo, where $lightbox_link1 = get_custom_field('lightbox_link1'). I'm fairly new to PHP.
Below is the defining function:
// Check for a lightbox link, if it exists, use that as the value.
// If it doesn't, use the featured image URL from above.
if(get_custom_field('lightbox_link1')) {
$lightbox_link1 = get_custom_field('lightbox_link1');
} else {
$lightbox_link1 = $image_full[0];
}
Echo Function:
<?php if ($lightbox_link1 = get_custom_field('lightbox_link1')) {
echo '';
} ?>
<?php if ($lightbox_link1 = get_custom_field('lightbox_link1')) {
should be
<?php if ($lightbox_link1 == get_custom_field('lightbox_link1')) {
= is used for assignment
== is used for comparison
=== is used for typesafe comparison
also you can't declare <?php ... ?> inside another <?php ... ?>
to get something like <?php ... <?php ... ?> ... ?>
take a look at what you did up to here:
<?php if ($lightbox_link1 = get_custom_field('lightbox_link1')) {
echo '<a href="<?php
Instead, using doublequotes in your echo statement will allow for the php variables inside to be parsed, so you could just do
echo "<a href='{$lightbox_link1}' data-rel='prettyPhoto[{$post_slug}]'></a>";
to get
<?php if ($lightbox_link1 == get_custom_field('lightbox_link1')) {
echo "<a href='{$lightbox_link1}' data-rel='prettyPhoto[{$post_slug}]'></a>";
} ?>

Passing a PHP Value from a link

I am new to PHP and learning. I'm trying to pass a value through a url link but it doesn't seem to work.
The link value I am passing is http://www.mysite.com/index.php?id=f
I want to run a js script if ID not F seen below but right now when I run it. It doesn't do anything:
<?php
$ShowDeskTop = $_GET['id'];
if (isset($ShowDeskTop)){
echo $ShowDeskTop;
if ($ShowDeskTop != "f"){
echo "ShowDeskTop Value is not F";
echo "<script type=\"text/javascript\">";
echo "if (screen.width<800)";
echo "{";
echo "window.location=\"../mobile/index.php\"";
echo "}";
echo "</script>";
};
};
?>
I know this is easy PHP 101 but I can't figure it out. I have tried everything from w3schools to other sites on Google for the answer and having no luck. Could someone please tell me what I am doing wrong?
Thank you!
$ShowDeskTop is not the same as $ShowDesktop variables names are case sensitive!
This is never gonna work since you set the variable AFTER checking if it exist..
The most easy way:
<?php
if (isset($_GET['id'])) {
echo $_GET['id'];
if ($_GET['id'] != 'f') {
?>
<script type="text/javascript">
if (screen.width < 800) {
window.location = "../mobile/index.php";
}
</script>
<?php
}
}
?>
I don't think <> is valid in PHP (it is in VB.NET ..) the is not operator is != or !== (strict/loose comparison).
Also you don't have to close if statements with a ;
This:
if (expr) {
}
Is valid and not this:
if (expr) {
};
I thought about writing != instead of <>.
You have a number of problems including bad variable case (i.e. variables not matching), checking for variables before they exist, etc. You can simply do something like this:
if (!empty($_GET['id'])) { // note I check for $_GET['id'] value here not $ShowDeskTop
$ShowDeskTop = $_GET['id'];
echo $ShowDeskTop; // note I change case here
if ($ShowDeskTop !== "f"){ // note the use of strict comparison operator here
echo "YES, the id doesn't = f";
echo "<script type=\"text/javascript\">";
echo "if (screen.width<800)";
echo "{";
echo "window.location=\"../mobile/index.php\"";
echo "}";
echo "</script>";
} // note the removal of semicolon here it is not needed and is bad coding practice in PHP - this is basically just an empty line of code
} // removed semicolon here as well
Fist thing, you need ; at the end of echo $ShowDesktop
And, what does f mean in if ($ShowDeskTop <> "f"){
use strcmp() instead of <> operator.
Try
if(!strcmp($ShowDeskTop, "f")){
echo "YES, the id doesn't = f";
}
<?php
$ShowDeskTop = $_GET['id']; // assign before checking
if (isset($ShowDeskTop)){
//echo $ShowDeskTop;
if ($ShowDeskTop !== "f"){
echo "YES, the id doesn't = f";
echo "<script type='text/javascript'>";
echo "if (screen.width<800)";
echo "{";
echo "window.location.replace('../mobile/index.php');"; // assuming your path is correct
echo "}";
echo "</script>";
}
}
?>

Categories