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 ;
}
?>
Related
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.
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>";
} ?>
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>";
}
}
?>
How can I write the following statement in PHP:
If body ID = "home" then insert some html, e.g.
<h1>I am home!</h1>
Otherwise, insert this html:
<p>I'm not home.</p>
Doing it with native PHP templating:
<?php if ($bodyID==='home') { ?>
<h1>I am home!</h1>
<?php } else { ?>
<p>I'm not home!</p>
<?php } ?>
You can try using this :
$html = '';
if ( $body_id === 'home' )
{
$html .= '<h1>I am home!</h1>';
}
else
{
$html .= '<p>I\'m not home.</p>';
}
echo $html;
This will echo the html code depending on the $body_id variable and what it contains.
You can use a switch command like so:
switch($body)
{
case 'home': //$body == 'home' ?
echo '<h1>I am home!</h1>';
break;
case 'not_home':
default:
echo '<p>I'm not home.</p>';
break;
}
The default means that if $body does not match any case values, then that will be used, the default is optional.
Another way is as you say, if/else statements, but if within template / view pages you should try and use like so:
<?php if ($body == 'home'):?>
<h1>I am home!</h1>
<?php else:?>
<p>I'm not home!</p>
<?php endif; ?>
Assuming $bodyID is a variable:
<?php
if ($bodyID==='home') {
echo "<h1>I am home!</h1>";}
else {
echo "<p>I'm not home!</p>";}
?>
Personally I think that the best way to do that without refreshing and without having to set a variable (like $body or something like that) is to use a javascript code, this because "communications" between JS & PHP is a one-way communication.
<script language="javascript">
<!--
if( document.body.id === "home" ){
window.document.write("<h1>I am home!</h1>") ;
}
else{
window.document.write("<p>I'm not home!</p>") ;
}
-->
</script>
otherwise you can build a form and then take the body.id value using $_GET function... It always depends on what you've to do after you now body.id value.
Hope this will be usefull & clear.
you can try in the following way:
$body_id = "home";
if ($body_id == "home") {
echo "I am home!";
} else {
echo "I am not home!";
}
or
$body_id = "home";
if (strcmp($body_id, "home") !== 0) {
echo 'I am not home!';
}
else {
echo 'I am home!';
}
Reference:
https://www.geeksforgeeks.org/string-comparison-using-vs-strcmp-in-php/
for some r.eason I cant display a logged in users name when they are logged in? the code is below
<?php
if (isset($_SESSION['user_id'])) {
echo '<?php if (isset($_SESSION[\'first_name\'])) { echo ", {$_SESSION[\'first_name\']}!"; } ?>';
if ($_SESSION['user_level'] == 1) {
echo 'something else';
}
} else { echo 'something';
}
?>
Thanks every one but i solved it.
Ack! Just look at your code. Do you know what this line is doing?
echo '<?php if (isset($_SESSION[\'first_name\'])) { echo ", {$_SESSION[\'first_name\']}!"; } ?>';
That's so wrong I don't even know where to begin. Just try
echo $_SESSION['first_name'];
And see if that gets you closer to what you want ;)
Make sure you're also calling session_start() before trying to access the variables.
Change your code to:
<?php
session_start();
if (isset($_SESSION['user_id'])) {
if (isset($_SESSION['first_name'])) {
echo ", " . $_SESSION['first_name']} . '!';
if ($_SESSION['user_level'] == 1) {
echo 'something else';
}
} else {
echo 'something';
}
?>
This is not a valid PHP code. Single quote "'" are not pair up. The block ('{' and '}') are also not pairing up.
The most importantly, the code to show the first name is in a string so it will not be shown.
I think the code you are trying to write is:
<?php
if (isset($_SESSION['user_id'])) {
if (isset($_SESSION['first_name'])) {
echo ", {$_SESSION['first_name']}!";
}
if ($_SESSION['user_level'] == 1) {
echo 'something else';
}
} else {
echo 'something';
}
?>l
Is it?
Here are the list of possibilities of the mistakes and make sure that you have corrected them
1) have you set the cookie "first_name" using setcookie method...?
2) Then have u called the session_start() function so that the session variables can be called in that page??
3) Try echo $_SESSION['first_name']... i don understand why you have put the flower brackets coz i never have used them even once in my 15 php projects..