Send variable to next page without overwriting - mysql

I want to pass $ids variable retrieved from db from page1 to page2 with session method but the code doesn't work or overwrites the variable.
page1
<?php
$queryString = "WHERE id='$id'";
$sqls = mysql_query("SELECT ids FROM markers $queryString");
$i=0;
while($row=mysql_fetch_array($sqls)) {
$_SESSION['ids'][$i]=$row['ids'];
echo $_SESSION['ids'][$i];
echo 'Modify<br />';
$i++;
}
//echo $_SESSION['ids'];
?>
page2
<?php
session_start();
include_once "scripts/connect_to_mysql.php";
$ids= $_SESSION['ids'].[$i];
//echo $ids;
?>
Thanks a lot for help.

you must do also session_start(); in page1 to work with sesion variables.
your code should work like that:
while($row=mysql_fetch_array($sqls)) {
$arrays[]=$row['ids'];
$_SESSION['ids']= $arrays[];
echo $_SESSION['ids'][0];
echo $_SESSION['ids'][1];
.......
echo 'Modify<br />';
}
page2
<?php
session_start();
include_once "scripts/connect_to_mysql.php";
$ids= $_SESSION['ids'];
echo $ids[0];
echo $ids[1];......
?>

Related

Display latest wordpress featured images on static html page

I have a wordpress blog page installed in subfolder of my website(converted from html)and a static html home page.
I would like to display 3 latest posts and its featured images on home page. With code below i can display latest posts text but i dont know how to show featured images of posts. Into index.php of a wordpress custom theme i placed featured photo inside a div:
<div id="blogphoto"><?php the_post_thumbnail(); ?></div>
This is the code on static html index.php page which is pulling out latest post. Can anyone help me to get featured images of these posts too?
<div id="wp-post">
<?php
$args = array('numberposts'=>1);
$recent_posts=wp_get_recent_posts($args);
foreach( $recent_posts as $recent_post ){
echo "<h3>".$recent_post['post_title']."</h3> <br>";
echo "<span>".$recent_post['post_date']."</span> <br>";
echo "<p>".$recent_post['post_content']."</p><br><br>";
}
?>
</div>
<div id="wp-post2">
<?php
$args = array('numberposts'=>1 , 'offset'=>1 );
$recent_posts=wp_get_recent_posts($args);
foreach( $recent_posts as $recent_post ){
echo "<span>".$recent_post['post_title']."</span> <br>";
echo "<p>".$recent_post['post_content']."</p><br><br>";
}
?>
</div>
<div id="wp-post3">
<?php
$args = array('numberposts'=>1 , 'offset'=>2 );
$recent_posts=wp_get_recent_posts($args);
foreach( $recent_posts as $recent_post ){
echo "<span>".$recent_post['post_title']."</span> <br>";
echo "<p>".$recent_post['post_content']."</p><br><br>";
}
?>
</div>
Please try to this code the_post_thumbnail getting the feature image
<?php
if ( $query->have_posts() ) {
$i = 1;
while($query->have_posts()) {
echo '<div id="wp-post-'.$i.'">';
$query->the_post();
?><h2><?php the_title(); ?></h2>
<?php
if ( has_post_thumbnail() ) { // only print out the thumbnail if it actually has one
echo '<p>post says it has a featured image</p>'; // double checking
the_post_thumbnail('thumbnail');
} else {
echo '<p>this post does not have a featured image</p>';
}
echo '</div>';
$i++;
}
} else {
echo '<p>no posts found</p>';
}
?>

Yii2: How to display Multiple images in view from path saved in database?

I have column called images in database which contain image paths.
Query:
$query = new Query;
$todo = (new yii\db\Query())
->select(['images'])
->from('room_types')
->andWhere("id = '$model->id'")
->all();
View :
<?php
foreach ($todo as $row)
{
?>
<?php echo Yii::getAlias('#web').'/'.$row; ?>
<?php
}
?>
Images path saved in db:
uploads/room_img/30.jpg;uploads/room_img/300.jpg;uploads/room_img/11928_569674493052762_732198968_n.jpg;
Tried with explode():
<?php
function room_images() {
$query = mysql_query("SELECT images FROM room_types WHERE id = $model->id");
while($row = mysql_fetch_array($query)) {
$e[] = explode(" ", $row[0]);
foreach($e as $r) {
echo $r;
}
}
}
?>
But nothing is showing
Use img method of Html class - reference
In your view file
use yii\helpers\Html;
// ...
<?php foreach ($todo as $key=>$row): ?>
<!-- html code if you need -->
<?php
foreach (explode(';', $row['images']) as $key_img => $value_img)
{
echo Html::img(Yii::getAlias('#web').'/'.$value_img);
}
?>
<!-- html code if you need -->
<?php endforeach; ?>
// ...

How do I restart my loop with get_next_post()?

I'm looking for a succinct method of making get_next_post()
double back to the beginning once it hits the last post.
Currently, it stops once it hits the final post.
Here are a few lines of code from the codex
for context that are similar to what I'm using:
<?php $next_post = get_next_post();
if (!empty( $next_post )): ?>
<a href="<?php echo get_permalink( $next_post->ID ); ?>">
<?php echo $next_post->post_title; ?>
</a>
<?php endif; ?>
http://codex.wordpress.org/Function_Reference/get_next_post
Thanks in advance for your suggestion.
you can't do it with get_next_post - full stop.
Here's how I've done it...
<?php
/**
* Infinite next and previous post looping in WordPress
*/
$next_post = get_next_post();
$previous_post = get_previous_post();
$current_id = get_the_ID();
// Get ID's of posts
$next_id = $next_post->ID;
$previous_id = $previous_post->ID;
$prev_title = strip_tags(str_replace('"', '', $previous_post->post_title));
$next_title = strip_tags(str_replace('"', '', $next_post->post_title));
// get the first posts ID etc
$args = array('post_type'=>'project', 'posts_per_page' => -1);
$posts = get_posts($args);
$first_id = $posts[0]->ID;
$first_title = get_the_title( $first_id );
// get the last posts ID etc
$last_post = end($posts);
$last_id = $last_post->ID; // To get ID of last post in custom post type outside of loop
$last_title = get_the_title( $last_id );
// if the current post isn't the first post
if($current_id != $first_id) { ?>
Previous Project:<?php echo $prev_title; ?>
<?php
// if the current post is the first post
} else { ?>
Previous Project
<?php }; ?>
<?php
// if the current post isn't the last post
if($current_id != $last_id) { ?>
<a rel="next" href="<?php echo get_permalink($next_id) ?>" title="<?php $next_title ?>" class="prev-next next"> Next Project <?php echo $next_title; ?></a>
<?php
// if the current post is the last post
} else { ?>
<a rel="next" href="<?php echo get_permalink($first_id) ?>" title="<?php $last_title ?>" class="prev-next next"> Next Project <?php echo $first_title; ?></a>
<?php } ?>
Elements take from here Getting first & last post in custom post type outside of loop

Wordpress Logout Link

How could I add a logout link in the else portion of code, after the "echo $upme->display();" ...
<?php
global $upme;
if (!is_user_logged_in()) {
echo $upme->show_registration();
echo $upme->login();
}
else { echo $upme->display();
}
?>
I tried a few things including the below code but I keep getting internal error ...
<?php
global $upme;
$html1 = 'Logout';
if (!is_user_logged_in()) {
echo $upme->show_registration();
echo $upme->login();
}
else { echo $upme->display();
echo $html1;
}
?>
Thank You
There's a small syntax error:
$html1 = 'Logout';
500 is generally an "I can't find that" error. What page is this from? get_permalink may be returning false. Try outputting that function to see what you get back.
<?php echo get_permalink(); ?>

Wrapping code in a function prevents it from working

I want to wrap my code in a function (and then put it in functions.php) so that I can call it elsewhere but my code fails as soon as I wrap it in a function.
I think this may be a scope issue, do I have to pass the the post number somehow to the function? If I get rid of the function that's wrapped around the query, the code works fine.
I'm guessing that the code is irrelevant really (although I may be wrong) - it's more to do with the fact that it's a loop and a function.
<?php function getGallery2() { ?>
<!-- 1. search for any pages with a custom field of 'test' that have a value of 'yes' -->
<?php query_posts('meta_key=Gallery - Promotion Gallery Photo Link&post_type=page'); ?>
<?php while ( have_posts() ) : the_post(); ?>
<!-- 2. echo the test field -->
<?php $link = get_post_meta($post->ID, 'Gallery - Promotion Gallery Photo Link', true); ?>
<?php $alt = get_post_meta($post->ID, 'Gallery - Promotion Gallery Photo Alt text', true); ?>
<img src="<?php echo $link ?>" alt="<?php echo $alt ?>" />
<?php endwhile;?>
<?php wp_reset_query(); ?>
<?php } ?>
<?php getGallery2(); ?>
You would have it something like this I think (not tested):
<?php function getGallery2() { ?>
$global post;
$link = get_post_meta($post->ID, 'Gallery - Promotion Gallery Photo Link', true); ?>
$alt = get_post_meta($post->ID, 'Gallery - Promotion Gallery Photo Alt text', true); ?>
<img src="<?php echo $link ?>" alt="<?php echo $alt ?>" />
<?php } ?>
Then call the function within any loop on any PHP page. Make sense? i.e. don't loop within the function. I don't understand why you don't just use a php include? i.e.
require('get-gallery.php');
Hope that helps :D
$post is not in the functions scope.
You can add global $post; to the top of the function or you can include it as a parameter like this:
function getGallery2($post){
// code
}
echo getGallery2($post)
Code inside a function can only see variables that were created within the same function or in global scope. Meaning the $post object is undefined.
//
On a slightly off topic note, you have lots of HTML comments within PHP. You could easily tidy things p by making it all PHP.
EDIT:
function getGallery2(){
global $post;
// 1. search for any pages with a custom field of 'test' that have a value of 'yes' -->
query_posts('meta_key=Gallery - Promotion Gallery Photo Link&post_type=page');
while ( have_posts() ) : the_post();
// 2. echo the test field -->
$link = get_post_meta($post->ID, 'Gallery - Promotion Gallery Photo Link', true);
$alt = get_post_meta($post->ID, 'Gallery - Promotion Gallery Photo Alt text', true);
echo '<img src="'.$link.'" alt="echo $alt " />';
endwhile;
wp_reset_query();
}
getGallery2();