php - pass variables through pages and changing it when go back - php

I'm making a web project in php and I have a small problem with understanding how I can get the variables to the next page after clicking on a button.
Concept
The concept is simple, - Questions asked by users(with title, the question, author, etc..) and other users can reply a specific question. So the idea is to pass all the info of the particular question into the page were it can be answer.
The most logical solution that I thought saving all the info of the post into $_SESSION so it would be more easy to pass to the second page(I think), but the problem is that when the user choose to go back to the previous page all the $_SESSiON should be destroyed if the user chooses another question to answer.
Problem:
I'm struggling to get that system working
side note:
Can it be made by using ajax?
What is the best system for this type os cases?
CODE
function display_perguntas(){
require 'dbh.inc.php';
$userID = $_SESSION['userID'];
$sql = "SELECT * from forum_pergunta where disciplinaID IN (select disciplinaId from users_disciplinas where userID = $userID) AND userID = $userID";
$stmt = mysqli_stmt_init($conn);
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result)){
echo '<div class="container">
<h2>Questions from my subjects</h2><br>
<div class="row">
<div class="col-xl-2 col-lg-3 col-sm-5">
Title: <strong>'.$row['title'].'</strong>
</div>
<div class="col-lg-3 col-xl-5 col-sm-5">
Author: '.$row['author'].'
</div>
</div>
<div class="row">
<div class="col-xl-2 col-lg-2 col-sm-3 col-3">
<h6>Question:</h6>
</div>
<div class="col-xl-9 col-lg-9 col-sm-9 col-9">
<textarea readonly style="width: inherit;">'.$row['question'].'</textarea>
</div>
</div>
<div class="row justify-content-between">
<div class="col-xl-2 col-lg-2 col-sm-4 col-5">
Subject name: '.$row['subjectID'].'
</div>
<div class="col-5">
Date: '.$row['date'].'
</div>
</div>
<div class="row" style="margin-top: 2%">
<div class="col-md-12 text-center">
<form action="" method="POST">
<button class="btn btn-success" type="submit" name="answers_question">Answer Question</button>
</form>
</div>
</div>
<hr>
</div>';
}
}

Given the question data is in the database I suggest using the query string $_GET for this (this also means your user can always navigate to an old question from their browser history).
To do this, you'd get rid of that form (that isn't really doing anything) and replace it with a link <a href="/answer_question.php?question_id="' . $row['id'] . '" class="button"> (assuming your row has an id called id and your css .button makes the link look like you want).
Then in answer_questions.php you just need to do $id = $_GET['id']; and then plug that into your database so you can display the question and build a form to answer it.

The most simple way is to use $_GET parameters.
The idea is to have the page with questions like:
/questions.php
And the question page:
/question-info.php?id=1
If you use mod_rewrite it can be
/questions/
/questions/1.html
And for better urls you can use slugs than ids

Related

PHP [ sorting data from database as collapsible bootstrap 4 cards ] [duplicate]

This question already has answers here:
List records grouped by category name
(2 answers)
Closed 2 years ago.
As the title says i'm developing shopping cart for a website and the cart contains all the products that been ordered from different companies like this :- screenshot
so as you see i'm trying to sort all products that is from the same company underneath each other as the specific company bill my question how can i accomplish that ?
what i have tried :-
nothing to mention actually i'm really so confused here i don't now what i'm going to (loop for or something like that .. )
i hope i explained what i want to accomplish, if not (screenshot)
code :- php PDOquery
<?
$accountid = '8';
require_once '..\Config.php';
// WHERE chemicalcom='$variable' OR name='$variable'
$dbCon = "mysql:host=$host;dbname=$db_name";
$variable = "Efexor";
$PDOCon = new PDO($dbCon, $username, $password);
$query = $PDOCon->prepare("SELECT * FROM basket WHERE accountid ='$accountid'");
$query->execute();
$basketItems = $query->fetchAll();
?>
code :- index.php
<? foreach($basketItems as $item){
echo'<h3>Bill('.$item['companyexported'].')</h3>
<div class="card">
<div class="row">
<div class="col-6"><img src="'.$item['imgpath'].'" class="productimg" alt=""></div>
<div class="col-auto">
<div class="card-title">
<div class="row">'.$item['name'].'</div>
<div class="row">'.$item['chemicalcom'].'</div>
<div class="row">'.$item['concentration'].'</div>
<br>
<div class="row">'.$item['price'].' $
</div>
<span class="badge badge-info qty">'.$item['qty'].'</span>
</div>
</div>
</div>
</div>';}?>
Thanks .
First, you will need to update your sql to order by the company, that will make the php simpler.
SELECT * FROM basket WHERE accountid ='$accountid' order by companyexported
then in your php, you only want to show the company once, so you create an empty variable and check it. If that variable doesn't contain a matching company name, show the company and set that variable to the current company name.
<?
$curr_co = "";
foreach($basketItems as $item){
if($curr_co != $item['companyexported']){
echo '<h3>Bill('.$item['companyexported'].')</h3>';
$curr_co = $item['companyexported'];
}
echo '<div class="card">
<div class="row">
<div class="col-6"><img src="'.$item['imgpath'].'" class="productimg" alt=""></div>
<div class="col-auto">
<div class="card-title">
<div class="row">'.$item['name'].'</div>
<div class="row">'.$item['chemicalcom'].'</div>
<div class="row">'.$item['concentration'].'</div>
<br>
<div class="row">'.$item['price'].' $
</div>
<span class="badge badge-info qty">'.$item['qty'].'</span>
</div>
</div>
</div>
</div>';}?>

Pass information from item to a item display page in PHP

OK so I'm doing a web application to sell games.
I've done a cycle to put all the games from the database in the index page.
Now when a user clicks a game it goes to the page with the game's info.
How can I save the info from the database in session variables to use in the other page ?
btw: JOGO = GAME
$stmt = $conn->query('SELECT * FROM JOGO');
foreach ($stmt as $row){
$_SESSION['id_jogo'] = $idJ;
echo '<link href="home/vendor/bootstrap/css/bootstrap.min.php" rel="stylesheet">';
echo '<link href="style.php" rel="stylesheet">';
echo '
<div class="col-lg-4 col-md-6 mb-4" >
<div class="card h-100 ">
<img class="card-img-top" src="http://placehold.it/700x400" alt="">
<div class="card-body">
<h4 class="card-title">
' .$row["nome"].'
</h4>
<h5>' .$row["preco"].'€ </h5>
<p class="card-text">' .$row["descricao"].' </p>
</div>
<div class="card-footer">
<small class="text-muted">Vendedor: </small>
</div>
</div>
</div> ' ;
}
?>
Storing game id in session is not an option. Line
$_SESSION['id_jogo'] = $idJ;
(though I don't know where $idJ comes from), I presume that you want to put game id in session, something like
$_SESSION['id_jogo'] = $row['id']; // assuming id is a primary key.
But this will not work, as this line just overwrites $_SESSION['id_jogo'] on every iteration.
Use standard approach with $_GET parameters, when your page url is itempage.php?game_id=42
In itempage.php you can get game id as $_GET['game_id'] and use it in a query.
So, your markup is something like:
<img class="card-img-top" src="http://placehold.it/700x400" alt="">
Assuming you’re initializing a session somewhere else in your code with session_start() you can create a new session variable name with $_SESSION[‘some_key’] = ‘data’;

How can I send Passport number in url through button?

I Just want to send passport number in the url on the next page. I know it will go into the url like this continue-to-pay.php?pno=$passportnumber".
But how will I post passport number on the next page.
<?php
include'connection.php';
$pno=$_GET['pno'];
$sql="SELECT * FROM user WHERE passportnumber='$pno'";
$query = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($query))
{
?>
<div class="col-xs-6 col-sm-6 col-md-6 col-lg-5 text-right" id="review">
<ul>
<li class="list">Passport Number </li>
</ul>
</div>
<div id="review2">
<ul>
<li class="list"><?php echo $row['passportnumber']; ?></li>
</ul>
</div>
<br><br>
<div class="col-xs-6 col-sm-12 col-md-12 col-lg-12" align="center"
valign="top">
<a href="e-visa-application.php?actionType=edit" class="btn btn-primary
btn-lg mgn-btm mgn-top ">Edit</a>
<a href="process.php" class="btn btn-primary btn-lg mgn-btm mgn-top
">Continue to pay</a>
</div>
if you got the PNO at your URL you can get it in a PHP file using $_GET :
<?php
if (isset($_GET['pno'])) {
echo $_GET['pno'];
}else{
// Fallback behaviour goes here
}
?>
You are probably searching for "The PHP session system" to keep the pno in the session while the user is going through the steps.
The PHP session system lets you store securely data in the $_SESSION global array. A example is to store the pno in the session.
session_start();
$_SESSION['pno'] = $pno;
Then, you can access that information on all other pages:
if (isset($_SESSION['pno']))
echo $_SESSION['pno'];
More info about sessions here
You can use $_REQUEST:
<?php
$_REQUEST['pno'] = $pno;
?>
you can use it to retrieve the pno in the continue-to-pay.php?pno=$passportnumber

Get an id to another page

Hello this is my first post here, I need your help.
First of all, I want to say sorry for my bad English. I hope you will understand me :D
So, I'm a newbie in php and I have a problem.
I want to take the id from a file, to another file, but I don't know how. I will show you my code below.
while($array = $db->get_row($row))
{
$id = $array['id'];
$Nume = $array['user_name'];
$date = date("Y-m-d H:i:s", $array['date']);;
$stitle = $array['shorttext'];
$text = $array['text'];
$logs .= '
<div class="post">
<div class="post-meta clearfix">
<h4 class="post-title left">
'.$stitle.'
</h4>
<p class="post-info right">
<span>'.$Nume.'</span>
'.$date.'
</p>
</div>
<div class="post-box">
<div class="post-content">
<div class="post-intro">
'.$text.'
</div>
</div>
<div class="post-footer clearfix">
<div class="continue-reading">
</div>
</div>
</div>
</div>
<br>
';
}
Here where is ID, I want to take that id to the news page. (the code is in index.php)
'.$stitle.'
In news.php have to select from mysql all but only the table with the ID from index.php
if(isset($_GET['id']))
{
$id = (int)$_GET['id'];
$row = $db->sql("SELECT * FROM `ucp_news` WHERE `id` = '".$id."' ");
$array = $db->get_array($row);
Can you help me, please?
Your PHP code is correct, although you should be using htmlspecialchars() when adding the variables to the output. This to prevent XSS attacks, and the like.
Though, your problem lies most likely in this line:
<a href="/index.php?page=news id='.$id.'">
More specifically, the missing ampersand (&) between news and id. Try to look at the value of $_GET['page'] when clicking on the link, then you should see what the problem is.

Php to auto populate grids

I have the following html code:
<div class="media row-fluid">
<div class="span3">
<div class="widget">
<div class="well">
<div class="view">
<img src="img/demo/media/1.png" alt="" />
</div>
<div class="item-info">
Title 1
<p>Info.</p>
<p class="item-buttons">
<i class="icon-pencil"></i>
<i class="icon-trash"></i>
</p>
</div>
</div>
</div>
<div class="widget">
<div class="well">
<div class="view">
<img src="img/demo/media/2.png" alt="" />
</div>
<div class="item-info">
This is another title
<p>Some info and details go here.</p>
<p class="item-buttons">
<i class="icon-pencil"></i>
<i class="icon-trash"></i>
</p>
</div>
</div>
</div>
</div>
Which basically alternates between a span class with the widget class, and then the widget class without the span3 class.
What I wanted to know was if there was a way to have php "echo" or populate the details for and details under the "item-info" class. Would I need to use a foreach statement to get this done? I would be storing the information in a mysql database, and while I can get it to fill in the info one by one (repeatedly entering the and echoing out each image and item title) it's not practical when the content needed to be displayed is over 15 different items. I'm not well versed in foreach statements so I could definitely use some help on it.
If someone could help me perhaps structure a php script so that it can automatically output the html based on the number individual items in the database, that'd be greatly appreciated!
I'm wondering if the html + php (not including the foreach) would look like this:
<div class="span3">
<div class="widget">
<div class="well">
<div class="view">
<img src="img/<? $file ?>" alt="" />
</div>
<div class="item-info">
<?$title?>
<p>Info.</p>
<p class="item-buttons">
<i class="icon-pencil"></i>
<i class="icon-trash"></i>
</p>
</div>
</div>
</div>
EDIT:
I wanted to add some more information. The items populated would be based on a type of subscription - which will be managed by a group id.
I was initially going to use <? (if $_SESSION['group_id']==1)>
echo <div class="item-info">
$title
<p>$info</p>
</div>
so that only the subscribed items would populate. But, I would need it to iterate through all the items for group1 table and list it. Currently I know that I can do
<? (if $_SESSION['group_id']==1)
while ($row=mysql_fetch_assoc($sqlItem))
{
$itemInfo = $row['info'];
$image = $row['image'];
$title = $row['title'];
$url = $row['url'];
};
>
$sqlItem for now can only be assigned one thing (manually - as in: $sqlItem = '123'), unless I iterate through which is what I'm trying to figure out.
Just read that 'mysql_fetch_assoc' is being depreciated with 5.5, here is the new way and looks better, easier I think.. Hope this helps, was updated today.
I hope this helps http://php.net/manual/en/mysqli-stmt.fetch.php
replace the printf with echo '//then your html stuff
This will iterate through the rows in your database until their are no more matching records.
shouldn't a while be enough? It depends on the structure of your database and website (we didn't need so much HTML I think. Some more PHP maybe). Hope this helps.

Categories