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)){
//...
}
Related
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>
I am working on a school project, a simple webshop.
I decided to ask it here.
What i want to do is display products from my database to my "product page" using PHP.
For some reason i only get the last product to show, also having trouble with the images.. I know the query is correct.
"SELECT * FROM products";
Here is my code.
This is my PHP, I have put this on top of the product.php page.
product.php
<?php
session_start();
include "includes/connectie.php";
include "includes/navbar.php";
$vraag = "SELECT * FROM products";
//var_dump($vraag); Var dump test
$resultaat = $conn->query($vraag);
if ($resultaat->num_rows > 0)
{ // Min. 1 row returned
while ($rij = $resultaat->fetch_assoc())
{
$id = $rij['id'];
$titel = $rij['product_name'];
$prijs = $rij['product_price'];
$omschrijving = $rij['description'];
$foto = $rij['image'];
}
}
else
{
// Do nothing
}
?>
This is my HTML code below, where i want to "display" i also have this in the same file as code above. So first php, then html
product.php
<body>
<div class="products">
<div class="productContainer">
<h1 id="banner">ALL ITEMS</h1>
<div class="productRow">
<?php
echo '<img src="img/product_'.$id.'.jpg">';
?>
</div>
<?php
echo '<h1>'.$titel.'</h1>';
echo '<p class="prijs">'.$prijs.'</p>';
echo '<p class="omschrijving">'.$omschrijving.'</p>';
?>
<div class="productRow">
<?php
echo '<img src="product_'.$id.'.jpg">';
echo '<h1>'.$titel.'</h1>';
echo '<p class="prijs">'.$prijs.'</p>';
echo '<p class="omschrijving">'.$omschrijving.'</p>';
?>
So i only put the HTML/PHP where i would want the first two products as example.
In total i have 6 products. I also included a picture of the database
database
What am i doing wrong?
This is how it looks right now:
example of product display
in your php file you have added all the values from database to an array. but you have not used any loop when you are trying to display array values in html. loop is a must to display all the values of array. Add a loop in your HTML file.
This is a fragment of my code, that get all users from Active Directory (some fields). The first column (user_name) is a reference to the page2.php where I can see User profile more detail. The problem is page1.php passes only last values of the link from the cycle. How can I do that, any dynamic link of the user passes its information. Could you help me? Thanks. $info is an array of LDAP data.
Here is my code:
<?php for ($i=0; $i<$info["count"]; $i++) {
$res=$info[$i];
$m_name=$res["displayname"];
$m_title=$res["title"];
$m_dep=$res["department"];
$m_tel=$res["telephonenumber"];
$m_mail=$res["mail"];
?>
<?php echo '<div class="column"><h5>'.$m_name[0].'</h5></div>'?>
<?php echo '<div class="column"><h5>'.$m_title[0].'</h5></div>'?>
<?php echo '<div class="column"><h5>'.$m_tel[0].'</h5></div>'?>
<?php echo '<div class="column"><h5>'.$m_tel[0].'</h5></div>'?>
<?php echo '<div class="column"><h5>'.$m_mail[0].'</h5></div>'?>
<?php } ?>
Depending on exactly what you are trying to do, you can do it in two ways. Based on what you described, and your comment about Sessions, you can pass information from page to page using a session.
http://php.net/manual/en/session.examples.basic.php
session_start();
$_SESSION['displayname'] = $res["displayname"];
$_SESSION['title'] = $res["title"];
$_SESSION['department'] = $res["department"];
$_SESSION['telephonenumber'] = $res["telephonenumber"];
$_SESSION['mail'] = $res["mail"];
And then on Page2, read the values back or use them directly on the page:
$name = $_SESSION['displayname'];
or
echo 'Hello ' . $_SESSION['displayname'] . ' how are you?';
But if Page1 is a list of all users, how would you determine what information to store in the session for passing to Page2?
It would make more sense to me to use GET parameters to pass information from page to page. So, for example, if Page1 is a list of all users, and Page2 information about a single user, you could structure your loop like this:
<?php for ($i=0; $i<$info["count"]; $i++) {
$res=$info[$i];
$m_name=$res["displayname"];
$m_title=$res["title"];
$m_dep=$res["department"];
$m_tel=$res["telephonenumber"];
$m_mail=$res["mail"];
?>
<?php echo '<div class="column"><h5>'.$m_name[0].'</h5></div>'?>
<?php echo '<div class="column"><h5>'.$m_title[0].'</h5></div>'?>
<?php echo '<div class="column"><h5>'.$m_tel[0].'</h5></div>'?>
<?php echo '<div class="column"><h5>'.$m_tel[0].'</h5></div>'?>
<?php echo '<div class="column"><h5>'.$m_mail[0].'</h5></div>'?>
<?php } ?>
The unique ID from LDAP is just that. Something unique to that user that you can use to search on Page2 and pull out all of the details you want to print. It should be present in your $res array already.
So Page2 would have something like this at the top:
if(!isset($_GET['id']){
//Give an error if the ID isn't in the URL
}
$id = $_GET['id'];
$result = loadFromLDAP($id);
//Do stuff with the resulting $result array
loadFromLDAP being a function you would write to get details about a user from LDAP. Hope this helps get you on the right track.
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?;
?>
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;