Get an id to another page - php

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.

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>';}?>

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

issue when ifisset added to code

some explenation
http://koopjesidee.nl/images/6755185029021696.jpg
in my database i have a column named new_label
when its a new product its set as new
when no new product the colums is empty
i want the class="main-label new-label" to be disabled when there nothing in that column
now as you see from the image, there is always a orange cirkel (as set in class main-label) even when column is empty
This is my code
<!-- select products from database -->
<?php
$query = "select * from contentwhere cat = 4 order by rand() LIMIT 18";
$result = $db->query($query);
while($row = $result->fetch_array())
{
$url =detail($row);
<!-- content on main page -->
echo '
<div class="item">
<div class="product-item">
<div class="main-label new-label"><span>'.$row['new_label'].'</span></div>
<div class="product-image"><img src="'.$row['picture_big'].'" alt="'.$row['name'].'"></div>
<div class="product-item-details">
<div class="product-item-name"> '.$row['name'].' </div>
<div class="price-box"> <span class="price">€'.$row['price'].'</span> <del class="price old-price">'.$row['from_price'].'</del> </div>
</div>
</div>
</div>';
}?>
I want add if isset like this so when there is no text in class=new-label class main-label new-label is disabled column in my database is new_label
<div class="item">
<div class="product-item">
<!-- ISSUE -->
if (isset($_row['new_label']!='')) {
<div class="main-label new-label"><span>'.$row['new_label'].'</span></div>;
} else {
<div class="main-label new-label"><span></span></div>;
}
<!-- HTTP ERROR 500 -->
<div class="product-image"><img src="'.$row['picture_big'].'" alt="'.$row['name'].'">
You cannot use isset() on the result of an expression, it can only be a variable. In fact, it will result in a fatal error if you try (which is probably why you received a 500 server error).
So you can probably change this line:
if (isset($_row['new_label']!='')) {
To this:
if (isset($_row['new_label'])) {
isset() returns a boolean anyway, so it doesn't make sense to compare it to a string.
Read more: http://php.net/manual/en/function.isset.php
SOLVED MYSELF GUYS
'.$row['new_label'].'';} ?>
thanks anyway

php pass id into modal

I'm stuck again! So hopefully you awesome people can help me...
So far what I have is a search function that queries the database, and returns results, which works fine, and then the user can click one of the results, which takes them through to a new detailed page of what they clicked, for example a brand. Then on the detailed page, it returns all of that brands details (using $_GET['id']) and a new set of data, from a relational table, with a list of all the offers corresponding to that brand, which also works. But then I get stuck, I want the user to be able to click on each offer, and have the details of that offer load into a modal... I've tried using the $_GET['id'] for the offers id, but obviously, with it being on the same page (i presume), it passes the id of the brand / parent instead of the offer.
This is the code that calls back all the offers relating to the brand
$offers_sql = 'SELECT * FROM codes WHERE client_id LIKE :client_id AND CURDATE() between start AND expiry';
$offersq = $db->prepare($offers_sql);
$offers_params = array(':client_id' => $id);
$offersq->execute($offers_params);
$results = $offersq->fetchAll(PDO::FETCH_ASSOC);
if(count($results) < 1) {
$output = "<h4>no current offers available</h4>";
}
else {
foreach($results as $r) {
$title = $r['title'];
$code = $r['textcode'];
$offer_id = $r['id'];
$expiry = $r['expiry'];
$date = new DateTime($expiry);
$output .= '<h4>' . $title . '</h4><hr>';
}
}
} catch (PDOException $e) {
echo "failed to load offers";
}
?>
and this is the code i was using for the modal
require('inc/connect/config.php');
include('inc/result.php');
$page_title = "Title - " . $name . " Offers";
include('inc/style.php');
include('inc/header.php');
include('inc/offers.php');
?>
<!-- Intro Section -->
<section class="intro-section">
<div class="container">
<div class="row">
<div class="col-lg-12">
<h3 class="navbar-fixed-top">Latest Deals</h3>
<h1><?php echo $name; ?></h1>
</div>
</div>
</div>
</section>
<section class="offer-section">
<div class="container">
<div class="row">
<div class="col-xs-12 col-sm-6 col-sm-offset-3 col-md-6 col-md-offset-3 col-lg-6 col-lg-offset-3">
<?php echo $output; ?>
</div>
</div>
</div>
</section>
<div class="md-modal md-effect-flip" id="offer">
<div class="md-content">
<h4><?php $offer = (int) $_GET['id'];
echo $offer ?></h4>
<button type="submit" class="modal-btn md-close navbar-fixed-bottom" value="submit"><p>close</p></button>
</div>
</div>
<?php include('inc/footer.php'); ?>
The code that returns the details of the brand is on a separate page, but i can include that if necessary.
What am i doing wrong? Please go easy on me, I am relatively new to php.
Thanks in advance
Kaylee
Make Changes, Where Output is declared in else condition. (Changes Done)
$output='<h4><a class="OfferIdClass" data-OfferID="<?echo $offer_id;?>" href="#offer" data-modal="offer" data-toggle="modal">'.$title.'</a></h4>';
// Add This to footer.php (Changes Done)
<div class="md-modal md-effect-flip" id="offer">
<div class="md-content">
</div>
</div>
//Add this below in that page, where you are clicking Modal.
//Changes Done
<script>
$('.OfferIdClass').click(function(){
var OfferID=$(this).attr('data-OfferID');
$.ajax({url:"Open-Offer.php?OfferID="+OfferID,cache:false,success:function(result){
$(".md-content").html(result); //Here, Changes Done
}});
});
</script>
//Create one Open-Offer.php page. (Changes Done)
Open-Offer.php
<?
extract($_GET);
<h4><?php $offer = (int) $_GET['id'];
echo $offer ?></h4>
<button type="submit" class="modal-btn md-close navbar-fixed-bottom" value="submit"><p>close</p></button>
?>

Categories