How to pass links with values to pahe2.php - php

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.

Related

Click on an image stored in a MySQL database table and get additional row content for that image

I have created a members.php page that connects to a database table. The table has the following fields: id, username, profile image.
The following PHP code displays the profile image for each user in rows of 6 and allows each image to be clickable.
<?php
// The code below will display the current users who have created accounts with: **datemeafterdark.com**
$result=mysql_query("SELECT * FROM profile_aboutyou");
$row1 = mysql_fetch_assoc($result);
$id = $row1["id"];
$_SESSION['id'] = $id;
$profileimagepath = $row1["profileimagepath"];
$_SESSION['profileimagepath'] = $profileimagepath;
$count = 0;
while($dispImg=mysql_fetch_array($result))
{
if($count==6) //6 images per row
{
print "</tr>";
$count = 0;
}
if($count==0)
print "<tr>";
print "<td>";
?>
<center>
<img src="<?php echo $dispImg['profileimagepath'];?>" width="85px;" height="85px;">
</center>
<?php
$count++;
print "</td>";
}
if($count>0)
print "</tr>";
?>
This is all great, however, when I click on the image that loads it re-directs me to: viewmemberprofile.php which is what it is supposed to do. But it always displays the same image with the same id value (i.e.) 150 no matter which image I click. What I would like to have happened is. If I click on an image with id 155 etc... it will display content for that image data field not consistently the same image data regardless of which image I click.
Your help and guidance would be greatly appreciated. Thank you in advance.
One thing that I forgot to mention is that I do use sessions so... when I am re-directed to the viewmemberprofile.php page I use the following code to aide in getting the data that I need from the table.
<?php
$id = $_SESSION['id'];
echo($id);
?>
<?php
echo('<br>');
?>
<?php
$profileimagepath = $_SESSION['profileimagepath'];
?>
<img src="<?php echo($profileimagepath);?>" width="50px;" height="50px;">
I have yet to impliment the suggested solution.
You need to pass the ID of the row to viewmemberprofile.php, e.g.:
<a href="viewmemberprofile.php?id=<?= $dispImg['profileimagepath'] ?>">
And viewmemberprofile.php needs to select that row from the DB:
SELECT * FROM profile_aboutyou WHERE id = $_GET['id']
The above SQL statement is pseudo-code; you need to write actual code to accomplish what it is describing, preferably using parameterized queries.

I want to display products from my database to HTML. I only get the last row from my database to show up

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.

Use session variable or copy PHP code from second page to third page of a website

I have three pages in the website. The first is login page,second is profile page and third is main page.
<?php
session_start();
$servername="blah blah blah";
$connectioninfo=array('Database'=>'mbr');
$conn=sqlsrv_connect($servername,$connectioninfo);
if($conn)
{
echo 'connection established';
}
else
{
echo 'connection failure';
die(print_r(sqlsrv_errors(),TRUE));
}
$q1="SELECT * FROM EmployeeTable WHERE EmployeeID = '" . $_SESSION['id'] . "' ";
$stmt=sqlsrv_query($conn,$q1);
if($stmt==false)
{
echo 'error to retrieve info !! <br/>';
die(print_r(sqlsrv_errors(),TRUE));
}
$row=sqlsrv_fetch_array($stmt);
echo $row['EmployeeName'];
$q2="SELECT * FROM pointsBadgeTable WHERE EmployeeID = '" . $_SESSION['id'] . "' ";
$stmt1=sqlsrv_query($conn,$q2);
if($stmt1==false)
{
echo 'error to retrieve info !! <br/>';
die(print_r(sqlsrv_errors(),TRUE));
}
$pbrow=sqlsrv_fetch_array($stmt1);
?>
The above is the php used in the second page of the website. Here I am using two queries $q1 and $q2 to retrieve information from two different tables (EmployeeTable and pointsBadgeTable) after connection to the database "mbr" here.
I then echo the desired Info in my html after retrieving info from the tables.
For instance,
<?php echo "". $row['goldTotal'] .""?>>
Here 'goldtotal' is a column in the pointsBadgeTable in the above php. Also please note that I am using " . $_SESSION['id'] ." here to show info only about the person who logs in the first page of the website.
The issue here is that I want to echo the same value in the third page as in second page. Will I have to write the same php code in third page as I wrote in second page or I need to store it in some session variable. How to use a session variable here?
Also, is it correct to rewrite the same code in third page also as in second page and use the same queries $q1 and $q2? I will copy and paste the same PHP to the third page also.
You Can include the second page in third page , you will get the value .
Example : file3.php
**<?php
include 'file2.php';
?>**

php - unwanted session results

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)){
//...
}

Grab variable from clicked anchor tag

I'm currently working on producing a basic content management system. In this code I'm grabbing a list of pages from the database 'posts' and then echoing them out. The purpose of the list is for users to be able to select an existing page and then edit the title & content, however I'm unsure on how I can take data from the selected link and then make my page-edit.php show the correct content to edit accordingly. I've assigned the variable $i to each of the anchor tags which increments throughout the while loop, ideally I'd use $i to SELECT the correct page from the database. I'd like to avoid using jQuery, although I'm assuming that's going to be suggested. Any insight would be great, thanks in advance.
<?php
$connection = mysql_connect('localhost', 'root', 'root');
mysql_select_db('posts');
$query = "SELECT * FROM pages";
$result = mysql_query($query);
$i = 0;
while($row = mysql_fetch_array($result)){
$i++;
echo '' . $row['page_title'] . ' - ' . $i . '' . '</br>';
}
mysql_close();
?>
Use the $i as a GET id variable in the link within the while loop:
echo '<a href="edit-page.php?id=' . $i . '>' . $row['page_title'] . ' - ' . $i . '</a>' . '</br>';
Then you can check for $_GET['id'] so that you know what id/page is to be edited and get the data for that from the database:
if (!empty($_GET['id'])) {
$query = 'SELECT * FROM pages WHERE id=' . $_GET['id'];
...
// Show the form populated with data from the above query.
}
I've kept this very simple to get you started.
Don't forget to look into protecting against SQL Injection and moving away from the deprecated MySQL API to use the MySQLi API or MySQL PDO.
Using this method, you'll need a to make an edit page for each page.
Main php file
<?php $db = mysqli_connect("localhost","user","pass","name");
foreach($this->db->query("SELECT * FROM pages") as $row): ?>
<a onclick="loadPage(<?php echo $row['id']; ?>)">
<?php echo $row['page_title']; ?>
</a>
<?php endforeach; ?>
<script>
function loadPage(id){
$.get('handler.php', { param: id })
.done(function (loaded){
document.getElementById("example").innerHTML = loaded;
});
}
</script>
Handler php file
<?php if(isset($_GET['param'])):
foreach($this->db->query("SELECT * FROM pages") as $row):
catch($_GET['param']){
case 1:
require_once 'edit-data.php';
break;
case 2:
require_once 'edit-data-two.php';
break;
default:
break;
}
endforeach; ?>
edit-data.php = edit the page X
edit-data-two.php = edit the page Y

Categories