Pass information from item to a item display page in PHP - 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’;

Related

how to call columns from database and format it with HTML code

Actually I am beginner programmer in HTML, CSS and PHP. I have simple website for add and register in courses. The user should be add course into website and the courses should be posted on the site.so users can browse and register.
Actually my problem is how to call the course name from database and how to format it with HTML code as I want.
This is the page of courses which is content the list of available courses in website ( please note it is only HTML code, I do that to see how the page will be )
Screenshot of page:
So as you see, the first page include many this HTML code to add course list into website with the following code:
<div class="card card-1">
<a href="http://127.0.0.1/project2/course details/course1.php">
<img src="http://127.0.0.1/project2/icons/coursepic.jpg" alt="Avatar" style="width:101% "></a> <div class="container">
<h4 class="textstyle"><b>Operating System</b> </h4>
<p class="textstyle">Free Course</p>
</div>
</div>
what i want do with PHP?
I want to write a PHP code to replace the P and h4 with the course name, cost of courses from my database for each available course.
Please note: the pic for each course it will be from my pc, no need to call the pic from database.
I tried to write PHP code like below:
<div>
<div class="card card-1">
<a href="http://127.0.0.1/project2/course details/course1.php">
<img src="http://127.0.0.1/project2/icons/coursepic.jpg" alt="Avatar" style="width:101% "></a> <div class="container">
<?php
include_once("db.php");
$result = mysqli_query(OpenCon(), "SELECT Course_Name,cost FROM `course`");
//while($res = mysql_fetch_array($result)) { // mysql_fetch_array is deprecated, we need to use mysqli_fetch_array
while($res = mysqli_fetch_array($result)) {
echo "<p>".$res['Course_Name']."</p>";
echo "<p>".$res['cost']."</p>";
}
?>
</div>
</div>
</div>
This is my result:
It's okay but I want the style to be like the first screenshot. each course should have picture.
After that when the user click on course name. I want move to another page which is content the course details ( for the same course that user clicked ) also it's called for my database
like this:
I hope any one help my to solve this problem only, I should solve this problem within 2 days only. and sorry if my explanation is bad.
Thanks in advance for everyone.
Put the code in a PHP loop.....
So, this
<div class="card card-1">
<a href="http://127.0.0.1/project2/course details/course1.php">
<img src="http://127.0.0.1/project2/icons/coursepic.jpg" alt="Avatar" style="width:101% ">
</a>
<div class="container">
<h4 class="textstyle"><b>Operating System</b> </h4>
<p class="textstyle">Free Course</p>
</div>
</div>
Becomes (after cleaning up the code a bit - I think you didn't mean to use two <p> in there, but I left them so you can see it. Note that using different lines for the segments makes it a lot easier to see what you have.)
include_once("db.php");
$result = mysqli_query(OpenCon(), "SELECT Course_Name,cost FROM `course`");
$count = 0;
while($res = mysqli_fetch_array($result)) {
$count ++;
// NOTE: Here is the LOOP! - not outside the query, but INSIDE it
// First you 'jump out' of PHP, going back to HTML
?> <!-- now you are in HTML (when you need PHP again, you 'jump in' and 'jump out' as needed - see the code below....) -->
<div class="card card-<?php echo $count;?>">
<a href="http://127.0.0.1/project2/course details/course<?php echo $count;?>.php">
<img src="http://127.0.0.1/project2/icons/coursepic.jpg" alt="Avatar" style="width:101% ">
</a>
<div class="container">
<h4 class="textstyle">
<b><p><?php echo $res['Course_Name'];?></p></b>
</h4>
<p class="textstyle">
<p><?php echo $res['cost'];?></p>
</p>
</div>
</div>
<?php // we are in PHP again....
}
That should do what you asked for - though I would go a step (well, more than one...) further and make as much of this dynamic as you can.
For this I will presume that:
your database table has a column called 'id' (if it doesn't, you should have) and it relates to the course number (you could make a course number column if they don't match up, but I'm keeping it simple)
you have all your pictures labeled 'coursepicX' where the X is the course number.
We'll use 'coursepic' as a default in case there isn't a picture yet...
Now, the code is more dynamic!
include_once("db.php");
$result = mysqli_query(OpenCon(), "SELECT id,Course_Name,cost FROM `course`");
while($res = mysqli_fetch_array($result)) {
// NOTE: Here is the LOOP! - not outside the query, but INSIDE it
// First you 'jump out' of PHP, going back to HTML
?> <!-- now you are in HTML (when you need PHP again, you 'jump in' and 'jump out' as needed - see the code below....) -->
<div class="card card-<?php echo $res['id']?>">
<a href="http://127.0.0.1/project2/course details/course<?php echo $res['id']?>.php">
<?php
$pic = "http://127.0.0.1/project2/icons/coursepic.jpg";
if(file_exists("http://127.0.0.1/project2/icons/course" . $res['id'] . ".jpg") {
$pic = "http://127.0.0.1/project2/icons/course" . $res['id'] . ".jpg";
}
<img src="<?php echo $pic; ?>" alt="Avatar" style="width:101% ">
</a>
<div class="container">
<h4 class="textstyle">
<b><p><?php echo $res['Course_Name'];?></p></b>
</h4>
<p class="textstyle">
<p><?php echo $res['cost'];?></p>
</p>
</div>
</div>
<?php // we are in PHP again....
}
Note that this is the basic 'shopping cart' sort of program - you will likely use it many (many) times in your career.
Happy Coding!

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

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

Add a unique style class to each mySQL entry on page?

I have a php function to retrieve the three most recent entries to a table that holds news highlights. Here is my function:
function getHighlights(){
return $this->query("
SELECT *
FROM `highlights`
ORDER BY `inserted` DESC
LIMIT 3
");
}
These highlights are then placed on my homepage via a foreach loop. Here is my code:
<?php foreach($highlights as $a){ ?>
<div class="col-md-6 highlight">
<div class="highlightItem">
<img class="highlight-backdrop" src="<?=$a->backdrop;?>">
<p class="highlight-title"><?=$a->title;?></p>
</div>
</div>
<?php } ?>
I'd like for my homepage to ALWAYS be in this format:
News Highlight Format. However, the only reason it's in that format right now is because the images have predefined sizes that fit nicely together.
I want to be able to reference the most recent news highlight and set that image to always display as 300 x 210. I also want to reference the next two highlights and set them to always display as 300 x 100.
What's the best course of action for this?
<?php foreach($highlights as $index => $a){ ?>
if($index == 0){
<div class="col-md-6 highlight">
<div class="highlightItem">
<img width="300" height="210" class="highlight-backdrop" src="<?=$a->backdrop;?>">
<p class="highlight-title"><?=$a->title;?></p>
</div>
</div>
}else{
<div class="col-md-6 highlight">
<div class="highlightItem">
<img width="300" height="100" class="highlight-backdrop" src="<?=$a->backdrop;?>">
<p class="highlight-title"><?=$a->title;?></p>
</div>
</div>
}
<?php } ?>
try to use that code

"if message/text exists in database", then show the message that the user created from latest to oldest

I'm working on this message system. The user has a form presented to him, where the user will type notes, in this case called a "message". When the user visits his profile page, he is shown the message that he had typed in the form earlier and pressed saved.
Screenshot:
http://i.stack.imgur.com/VEsDg.png
My database has a table called "messages" where I have columns pid, uid, message, time, and picture where the uid is the uid in my "users" table.
So the pid is a auto-incrementing and the uid is the user id of the user who posted the message.
(The user posts a URL to his picture in the form which is later saved to the picture column in the database.
My PHP code where I get data from the database:
<?php
$uid = $this->session->userdata('uid');
$this->db->limit(10); //Use this to limit the entires while you show the entires on the front page.
$query = $this->db->query("SELECT pid, message, time, picture FROM messages WHERE uid = '$uid';");
foreach ($query->result_array() as $row)
{
$message = $row['message'];
$time = $row['time'];
$picture = $row['picture'];
}
?>
My html code in my profile_body.php page in my views folder where I have the above's code included.
<div class="list-group list-group-breakout">
<a class="list-group-item" href="https://assembly.com/assemblycoins">
<div class="chip">
<div class="chip-icon">
<img alt="Coins" width="48" height="48" class="app-icon" src="<?php echo $picture ?>"/>
</div>
<div class="row">
<div class="col-sm-12">
<p class="omega"><?php echo $message;?></p>
</div>
<div class="col-sm-12 right-align">
<p class="gray-2 small omega">
Feb. 21, 2015 <!-- Not from database -->
</p>
</div>
</div>
</div>
How would I get only 10 results for the messages that the users typed in the database and display it? I have tried to paste the same html code again but it displays the same message and picture.
Thanks.
Here is some improvements to do :
$uid = $this->session->userdata('uid');
$result = $this->db->select('pid, message, time, picture')
->where(array('uid' => $uid))
->limit(10)
->get('messages')
->result_array();
$this->load->view('profile_body', $result);
In your view profile_body.php file :
<?php foreach($result as $row): ?>
<div class="list-group list-group-breakout">
<a class="list-group-item" href="https://assembly.com/assemblycoins">
<div class="chip">
<div class="chip-icon">
<img alt="Coins" width="48" height="48" class="app-icon" src="<?php echo $row['picture'] ?>"/>
</div>
<div class="row">
<div class="col-sm-12">
<p class="omega"><?php echo $row['message'];?></p>
</div>
<div class="col-sm-12 right-align">
<p class="gray-2 small omega">
Feb. 21, 2015 <!-- Not from database -->
</p>
</div>
</div>
</div>
<?php endforeach; ?>

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