The code below stores array of post id's into session and using foreach I iterate over post id and call data(title,body) related to that post id. It works fine.
index.php
$result = $query->fetchAll(PDO::FETCH_COLUMN, 0);
$_SESSION['postid']=$result;
foreach($_SESSION['postid'] as $key=>$postid){
$sql = "SELECT * FROM posts WHERE postid=:postid";
$query = $db->prepare($sql);
$query->execute(array(':postid'=>$postid));
$row = $query->fetch(PDO::FETCH_ASSOC);
<?php echo $row['title']; ?>
<p><?php echo row['body'] ?></p>
}
Now I want to get directed to post page by clicking on title link from index page, and call that specific post id from session array whose title was clicked. Using $postid = $_SESSION['postid']; on post page gives the whole array of post id's . Is there any way by which I can start session on post page for only that specific post id whose title was clicked on index page.
In index.php page, modify the anchor tag like this :
<?php echo $row['title']; ?>
then on post page use this code to set postid in session
<?php
$_SESSION['pid'] = isset($_GET['pistid'])$_GET['pistid']:null?;
?>
Related
I have a page called profiles.php that displays the users own information when logged in. Recently I made it so the user can click on the name of someone else and it will take them to profiles.php. I want it to display the user's they clicked on profile/information, but it only shows your own information.
The way I have the page now is, it uses a session varaible based on if your logged in and from there it puts your data out in its designated places if that makes sense.
//if theuser is logged in then it turns their database id into a variable for later use
if(isset($_SESSION['userUid'])){
$current = $_SESSION['Id'];
}
//This is the link a person clicks on thats supposed to take them to that user's page. $post is a varaible from a foreach. I tried to make it so once they click it takes them to profiles.php users id (href = "profiles.php/id"'.$post["idUser"].'")
<a href = "profiles.php/id"'.$post["idUser"].'" ><h1>'.$post["UserID"].'</h1></a>
I expect the output to be taking the the current user to the desired user's profile page, but the actual output is taking the current user to their own profile page.
From what I understand you would like to show specific users profile information.
// define your connection
require_once('connection.php');
$users = mysql_query($conn, "Select id, name from users);
$row = mysql_fetch_array($users)
?>
<ul>
<?php
while($row){
echo <? <a href='profile_display.php?id=<?php $row['id'] ?> > <li> <?php $row['name'] ?> </li></a> ;
}
</ul>
in profile display.php
<?php
require_once'connection.php' ;
$id = $_GET['id'];
if(isset($id)) {
$result = mysql_query($conn, 'select * from users where id=$id);
if(!empty($result)) {
// display all value here
} else {
echo "No user profile information was found!"
}
?>
This code is not tested by me. But I would say this is the strategy needed to be used here.
Pseudocode:
List all users
User click specific other user, grab their id
make a query on db for the user information
display if available else display error information
Hope this helps!
I have created an application and I want to transfer unique values/id from one page to the another. I have used session for this process.
Below code:
<?php session_start();?>
<?php
#php code to get values
include_once "process/config.php";
$sql = "select * from posts";
static $i=0;
$result = mysqli_query($conn,$sql);
if($result->num_rows > 0){
while($rows = $result->fetch_assoc()){
$_SESSION["post_row_id"] = $rows["id"]; #Storing post id for updation.
echo $_SESSION["post_row_id"];
?>
<tr class="gradeA">
<td><?php echo ++$i; ?></td>
<td><u><a onmouseover="this.style.color='blue'" onmouseout="this.style.color='black'" href="post_action.php">
<?php echo $rows["post_title"]; ?></a></u></td>
</tr>
<?php
}
}
?>
I want to transfer row_id to the post_action.php page with $_SESSION["post_row_id"] but each and every time i click anchor tag the last id is transfered to the next page.
but when i echo $_SESSION["post_row_id"] all the unique values are shown.
Note:
I did by passing id in the anchor tag its working fine. but in the URL the value is getting displayed and anyone can change that value and pass some other data. Session's are safe at that point, so I was trying to do it with the session
To stick with your original code,
you can pass parameters on your <a href> tag,
by adding something like this to your url,
if your goal is just to pass that id to another page, you may remove the session since its no longer needed since the id is already passed on your url,
<?php session_start();?>
<?php
#php code to get values
include_once "process/config.php";
$sql = "select * from posts";
static $i=0;
$result = mysqli_query($conn,$sql);
if($result->num_rows > 0){
while($rows = $result->fetch_assoc()){
$_SESSION["post_row_id"] = $rows["id"]; #Storing post id for updation.
echo $_SESSION["post_row_id"];
?>
<tr class="gradeA">
<td><?php echo ++$i; ?></td>
<td><u><a onmouseover="this.style.color='blue'" onmouseout="this.style.color='black'" href="post_action.php">
<?php echo $rows["post_title"]; ?></a></u></td>
</tr>
<?php
}
}
?>
For your requirement you need to pass the current row unique id so use it like this using GET method. for security purpose validate it on server side
<a href="post_action.php?row_id=<?php echo $rows["id"]; ?>"<?php echo $rows["post_title"]; ?></a>
Note : you can able to store all id in session .but you can't find which row is clicked #JasshhAndrews
i am developing a web application,which has 3 pages.
first is index.php
which has search bar on which user searches.
second is search.php
which displays the search results like result_1,result_2,result_3 with info(title,description,url) when user click on any result it send the user to final page i.e show.php
and third page is show.php
on which info is displayed for the result which user has clicked.
for eg(corresponding url content will be displayed using iframe)
i tried using two dimensional session array,which is working not correctly.
when user click on any result,some other result's info is displaying on show.php
i check the session array content by print_r,it had unnecessary content.
someone help me in this i am sharing my code snippet.
search.php
<?php
session_start();
$id = 1;
while($row = mysqli_fetch_array($sql))
{
$title = $row['title'];
$description = $row['description'];
$url = $row['content_url'];
$icon = $row['thumb_icon_url'];
$_SESSION['result'][] = Array('title' => $title,'description'=> $description,'content_url' => $url,'icon' => $icon,'id'=> $id);
?>
<li name="id" >View doc</li>
<?php
$id++;
?>
show.php
<?php
if(isset($_GET['id']))
{
$id = $_GET['id'];
?>
<div>
<iframe class="embed-responsive-item item" src="<?php echo $_SESSION['result'][$id]['content_url'];?>"></iframe>
</div>
when i tried to check $_SESSION['result'] i got this
this array should have contain only query results.Help me to fix it
you're not setting the key of your array:
$_SESSION['result'] = Array();
$_SESSION['result'][$id] = Array('title' => $title,'description'=> $description,'content_url' => $url,'icon' => $icon,'id'=> $id);
add session_start(); in your show.php
If I understand correctly, your problem is that you have unwanted results.
This is basically because your are always adding results to the $_SESSION['results'] across all your queries.
Every time that you run search.php you will keep adding items to $_SESSION['result'], breaking the correspondence of the $id and the position of the array.
I would initialize the $_SESSION['result']
<?php
session_start();
$id = 1;
$_SESSION['results'] = [];
while($row = mysqli_fetch_array($sql)){
//...
}
Currently I'm making a blog out of scratch.
I already have a login page and a "create post page". But when I create a post I just store the information inside the database.
How can I make it that I create a new page for the blog post?
Something like: [sitename].com/post.php?id=1
Thanks for helping :)
Interesting question. Here I have an example on how you could do that (PDO). $_GET["id"] will take the ID from the URL. As example domain.com/post.php?id=[getid]
<?php
$id = $_GET["id"];
$sql = "SELECT * FROM `posts` WHERE `id` = $id";
$result = $conn->query($sql);
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
echo $row["title"].' '.$row["content"];
}
?>
And if you want to link them back to the homepage when the ID is not given, you could use this;
if (empty($_GET["id"])) {
header("Location: index.php");
}
For linking from the index to the posts you can use;
$sql = "SELECT * FROM `posts` ORDER BY `id` DESC";
$result = $conn->query($sql);
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
echo '<a href="posts.php?id='.$row["id"].'">';
}
}
In your post.php file, you can load the content from a specific post using the id provided in your url.
<?php
$postid = $_REQUEST['id'];
$post = someSqlFunction($postid); // <-- Replace with database code
echo '<h1>' . $post['title'] . '</h1>';
So if you wanted to load the post with id 15, you simply go to [sitename].com/post.php?id=15
You can create a page for each and every blog post you create. When you create a blog post there must a submit or post, after submitting it goes to a php file where you write the code to store the contents of the blog post. In that very file you write some code to create html file (file programing) for blog post having particular id. And you add all the code of css,html,also contents and write to that file and save it as .html format(eg. BlogPost_1.html etc). You can store file names in database for later use.
I hope thats what you needn
I'm retrieving products from my database and listing them in a while loop. Like this
<?php
while($row = mysql_fetch_row($result)){
echo '<div class="product_info">';
echo '<div class="category_product_title">Product';
echo '</div>';
echo '<div class="category_product_number">#'.$row[0].'('.$row[1].')'.'</div>';//prints id and name
echo '<div class="category_product_description">'.$row[2].'</div>';//prints description
echo '<div class="category_product_price">'.$row[4].'TL</div>';//prints price
echo '<div class="category_details">DETAILS</div>';//The link to list the product details
echo '</div>';
echo '</div>';
}
?>
I want to be able to print more details on a paticular prodcut when the "DETAILS" link is clicked by getting the id or name of that paticular product and using to query the database.
I tried saving the id in a session variable like this $_SESSION['id'] = $row[0] but since i'm looping through these products, i'm not able to get the id of a specific product when it is clicked. How can i do this?. I'm confused because of the loop. Thanks
You can assign a different link to each one of the products with a GET variable.
Instead of DETAILS, the link could point to DETAILS.
Then, on the product page productpage2.php, you can get the selected ID from the $_GET superglobal array like this:
$id = $_GET['id'];
Lots of ways to do this. I would probably do something like:
echo '<div class="product_info" data-product-number="' . $row[0] . '">';
Then just grab the data-product-number attribute with JavaScript and pass it along with the request. Obviously, you'll need to verify this on the server side somehow once the request is made, since a user could set the product number in the request to anything they'd like.