PHP linking from one page to the next - php

I am trying to create a small website to sell items on and learn php. I have a index.php which links to a chairs.php page and then from there to a viewItem.php page depending on which item they choose.
I have in my database 4 items. On the chairs.php a while loop runs to display on the page all items in that database.
I would like to use the id of the item to link to the next page but I think the while loop is always going to assign to the variable that I am using the very last entry in that while loop.
Can someone point me in the direction of how I would resolve this? I would like the user to select a Chair 1 on my Chairs.php page with ID 1 in my db to link to the corresponding page. Currently all links on the chairs.php would link to the last item in my while loop which is then assigned to the $_SESSION['output_id'] = $output_id variable.
Thank You
<?php
$query = "SELECT * FROM `chairs_table";
$queryChairs = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($queryChairs)) {
$output_id = $row["ID"];
$output_img = $row["Image"];
$output_name = $row["Name"];
$output_desc = $row["Desc"];
$output_price = $row["Price"];
$_SESSION['output_id'] = $output_id;
$_SESSION['output_img'] = $output_img;
$_SESSION['output_name'] = $output_name;
$_SESSION['output_desc'] = $output_desc;
$_SESSION['output_price'] = $output_price;
?>
<div class="itemContainer" onclick="location.href = 'viewItem.php?id=<?php echo "{$output_id}" ?>'" style="cursor:pointer;" >
<div> <img src = "img/<?php echo "{output_img}" ?>"> </div>
<div class="itemTextTitle"><?php echo"{$output_name}" ?></div>
<div class="itemTextDesc"><?php echo"{$output_desc}" ?></div>
<div class="itemTextPrice">cop <?php echo"{$output_price}"?></div>
</div>
<?php
}
?>
</div>

Related

Generate an accordion from PHP and MySQL

Okay guys this is going to be one long question so please bear with me.
I want to create an accordion with contents in it:
How I want the image to look like
How it looks right now
I have a web application, where I upload various types(blog, homepage) of images every week. The first week is round 1 and the image can be labelled something like 1.x.x where 1 is the number corresponding to the week. Likewise for 2.x.x, 3.x.x etc.
I insert these images to a server using php and log the entry into a database, with information like project_name, file_name, url, version etc.
What I want to do is, automatically generate multiple accordion tabs one below the other. First tab should be the round corresponding to the latest week of uploading. Below that the week before and like wise till it reaches round 1(See images attached).
Now I am able to pull data and display files from ALL the rounds in a single round. What I want to do is separate rounds, automatically based on version number.=>> 1.x.x files in Round 1, 2.x.x in round 2 etc
Following is the code that I have written. The commented part is what I have been follow and and do. I am a total new comer to PHP and would appreciate it if anyone could help me with this. Thanks!
$project_name_download_form = $_GET['project_name'];
$file_name_download_form = $_GET['file_name'];
$version_download_form = $_GET['version'];
$download_data = "SELECT *
FROM projects, files
WHERE project_id = projects.id
AND project_name = '$project_name_download_form'
AND file_name = '$file_name_download_form'
AND version = '$version_download_form'";
mysqli_select_db($conn, $GLOBALS['database']);
$return_data = mysqli_query($conn, $download_data);
if (!$return_data) {
die('Could not get what you wanted: ' . mysqli_error($conn));
}
while ($row = mysqli_fetch_array($return_data)) {
$url = $row['url'];
$project_folder = $row['project_folder'];
//$id = 18;
$file_name = $row['file_name'];
$version = $row['version'];
$details = $row['details'];
$file = $row['file'];
$new = basename($row['file']); // GET FILE NAME ONLY, GET RID OF PATH.
// $design->url = $row[file_name]
// $design->version = $row[version]
// $designs.push ($design)
}
mysqli_free_result($return_data);
// $lastRound = 0;
// for each $design in $designs
// {
// $round = $design.version.split('.')[0];
// if $round <> $lastRound {
?>
<div class="col-sm-2" id = "accordianSet">
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapseOne"><span>
</span>Explore</a>
</h4>
</div>
<div id="collapseOne" class="panel-collapse collapse">
<div class="panel-body">
<?php
}
?>
<?php
// <HTML FOR LINK >
}
echo "<table class = 'table'>";
echo "<tr>";
echo " <th>File Name</th>
<th>File Link</th>";
echo "</tr>";
What you want to do is pull the results for each week and loop through but each time you loop you want to print variables in html like follows:
<?php
$i=0
while ($row = mysqli_fetch_array($return_data)) {
$url = $row['url'];
$project_folder = $row['project_folder'];
//$id = 18;
$file_name = $row['file_name'];
$version = $row['version'];
$details = $row['details'];
$file = $row['file'];
$new = basename($row['file']);
$i++
//1st Round
echo' <table>
<caption> Round $i</caption>
<tr>
<th>File Name</th>
<td>$file_name</td>
</tr>
<tr>....
</tr>
</table>';
}//while loop close tag
Note that you can format the data anyway you want but you have to spit out the html as you loop through the mysql results. Hope this helps.

Get the ID of my clicked product

I'm new to this site and wondering if somebody could help. I'm creating a website using bootstrap. I added some products in my DB and have used a while loop to display them on the page.
If you click on one of the products it takes you too a new page which should display all the information from only that product.
if ($select_db) {
$SQL = "SELECT * FROM products";
$result = mysql_query($SQL);
?>
<?php
$c = 0;
$id = -1; // The counter
$n = 3; // Each Nth iteration would be a new table row
while ($db_field = mysql_fetch_assoc($result)) {
$itemName = $db_field['name'];
$itemDescription = $db_field['description'];
$itemPrice = $db_field['price'];
$myPic = $db_field['image_name'];
if ($c % $n == 0 && $c != 0) { // If $c is divisible by $n...
echo '<div class="row" ></div>';
}
$c++;
$id++;
?>
<div class="col-md-4 col-sm-4" style="background-color:lavender;" id = 1>
<a href="productInfo.php">
<p> <?php echo $c ?> </p>
<h2> <?php echo $itemName ?> </h2>
<p><img class="img-responsive" img src= '<?php echo $myPic ?>' alt="Oops, Image cannot be found!" height="300" width="300"/></p>
<h3><?php echo $itemDescription ?></h3>
<p><?php echo $itemPrice ?></p>
<div id="selector" class="btn-group">
<button type="button" class="btn btn-primary" id="<?php $id ?>">Add</button>
</div>
<?php
$productsArray[] = array(
"id" => $id,
"itemName" => $itemName,
"itemDescription" => $itemDescription,
"price" => $itemPrice
);
//$_SESSION['sessionArray']=$productsArray;
?>
</a>
</div>
<?php
Now when I click on one of the columns it takes me to productInfo.php. I'm stuck as to how to display the product that is clicked?
I have added a variable $id which is the same as the array, example array [0] has id of 0, array [1] has id of 1. I think I'm trying to say if ID = 0 then display the results in the array at place [0]. I'm not sure if this helps?
I tried to display just the $id for now on productInfo.php but it only shows the last $id in the array
<?php
session_start();
if (isset($_SESSION["id"])) {
$newID = $_SESSION["id"];
echo $newID;
}
?>
I know this because it doesn't know which one I'm selecting. Am I making this too complicated?
Any help would be appreciated greatly!
Within your while loop I would add:
View More
This would concatenate the value of $id++ into the href under each item. Then all you need to do is create a landing page for product_page.php, define the $id on that page again and pull the data for that product from the db. You can do this without using any arrays.
Edit:
You would define $id on your product_page.php using $_GET['id'], as it is in the url, supplied by your href above ^
A good practice to get into whilst in the development stages would be to echo $id; to see if it is echoing the correct data from your while loop. If it echo's the correct $id then you can send the $id to the url through the href.
Let me know if this works or not.
Pulling data from db of your ID:
$sql = "SELECT * FROM table WHERE id='".$myID."'";
$res = mysql_query($sql);
$row = mysql_fetch_assoc($res);
$data1 = $row['whatever'];
echo $data1;

show all users from table by pagination

i got large amount of users into my table and i want to made an small script to show all users as 10 per page
I'm using this script http://www.phpeasystep.com/phptu/29.html but its showing only 1 result per page.
As they are saying on pic 7 i loop the code to show the users as this :
$id = $row["id"];
$username = $row['username'];
$date = $row['date'];
And its echo-ed as this:
<?php
echo '<div class="content">
<h1> ' . $username . ' </h1>
<div class="content_item">
<p>'.$date.'</p>
</div>
</div>';
?>
However i dont understand why its showing only 1 result per page!
I setted $start as 0 and $limit as 10
You have to place this echo code in a Loop, which would echo the Details of Users repeatedly.
For Example-
foreach($results as $r)
{
echo $r->id;
echo $r->username;
}

Automatically update a <div> part of an HTML if a new database record is added

I have a simple page which contains some products in the database. What I want is to create some sort of an updater, if the user add a product then that specific div in the page will display the latest product added automatically.
I tried to view this link but it doesnt help at all we have different problem.
Anyway heres the function i have in php.
function recentlyAddeditems() {
echo '<h3>Recently Added Products</h3> <hr/>';
echo '<div id="main_section_widget">';
$sql = mysql_query("SELECT * FROM products ORDER BY id DESC LIMIT 20") or die(mysql_error());
if ($sql) {
while ($row = mysql_fetch_array($sql)) {
$name = $row['name'];
$id = $row['id'];
$category = $row['category'];
echo "$name | $category<br/>
<hr/>
";
}
echo '</div>';
echo '
<p><br/>More items...</p>
';
}
}
and the in html page
<div id="main_section_widget_cover" class="dates">
<?php recentlyAddedItems(); ?>
</div>
Is there a simpler way how to do this? I tried to read about ajax in jquery but still having confused without a concrete example.
Please help wizards of stack!!!
IF you want an example of a ajax in jquery here is a really simple example
<div id="main_section_widget_cover" class="dates"><div>
<script>
$('#main_section_widget_cover').load('recentitems.php');
</script>
in recentitems.php
<?php recentlyAddedItems(); ?>
Thats it, thats all you need to do to do a basic ajax call

Using PHP (and maybe AJAX?) with jQTouch

I am having some difficulty using PHP with jQTouch. I am fairly
confident with JavaScript however my PHP skills are little to none.
I am creating an application for my final year project at University
what displays football rumours posted by different users. My problem
is as follows:
I have one screen that displays each individual rumour, using a while
loop in PHP I am able to get each rumour from the database and display
them correctly. However I want to be able to click on one rumour which
then displays this rumour in a different screen, along with options to
reply/share etc. However I do not know how to tell which rumour has
been clicked on.
Snippets of my code:
All rumours page:
<?php
$q1 = "SELECT * FROM tblrumours;";
$r1 = mysql_query($q1);
while( $row1 = mysql_fetch_assoc($r1) ){
?>
<a class="rumourTag submit" id="<?php echo $row1['rumourID']; ?>">
<div class='oneRumour'>
<div class='standardBubble'>
<p>
<?php
$userID = $row1['userID'];
$q2 = "SELECT * FROM tblusers WHERE userID = $userID;";
$r2 = mysql_query($q2);
while( $row2 = mysql_fetch_array($r2) ){
$username = $row2['username'];
$teamID = $row2['teamID'];
}
$q5 = "SELECT * FROM tblteams WHERE teamID = $teamID;";
$r5 = mysql_query($q5);
while( $row5 = mysql_fetch_array($r5) ){
echo "<img src='img/".$row5['teamPicture']."' alt=''
class='teamImg' />";
}
?>
<span class='username'>
<?php
echo $username;
?>
</span>
<br/>
<span class='rumourMsg'><?php echo $row1['rumourText']; ?></
span>
</p>
</div>
</a>
SINGLE RUMOURS PAGE:
<?php
$q1 = "SELECT * FROM tblrumours WHERE rumourID = 1;"; /* NEED
TO SELECT WHERE RUMOUR ID IS THE ONE THAT IS CLICKED */
$r1 = mysql_query($q1);
while( $row1 = mysql_fetch_array($r1) ){
?>..........
I have tried using Session variables, storing the ID's in an array,
creating a separate php file for the single rumour page, and all to no
avail. I am guessing I have to use AJAX in some way, but I have no
idea where to even begin. Any help is greatly appreciated!
Thanks!
If you need to click on a rumour to see more details about it, you could always output in the HTML a unique value used to reference that rumour in the DB.
e.g. have <span class='rumourMsg' id='rumourName'> where rumourName is a unique value stored in your database to reference that rumour. Then when a user clicks to see more details, you can make a request to the PHP page with that value and return the content.
e.g. rumourDetails?rumourName=uniqueRumourName
(make sure to escape all your data properly to avoid SQL injection vulnerabilities.)

Categories