create a clickable link in MySQL results - php

I'm in the process of learning some coding. I am looking to create the results of MySQL results clickable. I would like the name (first and last name) to be "clickable" so they can link to the customer record to view. Thanks in advance for your help!
<table class="list"; >
<tr>
<th>Name</th>
<th>Address</th>
<th>Email</th>
<th>Phone</th>
</tr>
<?php while($customers = mysqli_fetch_assoc($customer_set)) { ?>
<tr>
<td><?php echo h($customers['first_name'].' '.$customers['last_name']); ?></td>
<td><?php echo h($customers['address1']. ' '.$customers['city'].', '.$customers['state'].' '.$customers['zip']); ?></td>
<td><?php echo h($customers['email1']); ?></td>
<td><?php echo h($customers['phone1']); ?></td>
</tr>
<?php } ?>
Thanks,

If you want clickable first name and last name then you need to add anchor tag with view url like below examples.
Example (send first name and last name separately)
<td><?php echo h($customers['first_name'].' '.$customers['last_name']); ?></td>
Better solution is you can send id of user . For example i am asuming you have one column id in customers table then you can pass id like below example
<td><?php echo h($customers['first_name'].' '.$customers['last_name']); ?></td>
Note: You can encrypt id by using some hash methods to securely pass id of user.
Now when you click on these links you will redirect on view page where you can get these id or firstname, lastname by using GET method.
Example
view-record.php
<?php
if(isset($_GET['uid'])) {
$userid = $_GET['uid']; //please add some validation before using this value.
}
?>

You need to use the <a> (anchor) html tag like this:
<table class="list"; >
<tr>
<th>Name</th>
<th>Address</th>
<th>Email</th>
<th>Phone</th>
</tr>
<?php while($customers = mysqli_fetch_assoc($customer_set)) { ?>
<tr>
<td><?php echo h($customers['first_name'].' '.$customers['last_name']); ?></td>
<td><?php echo h($customers['address1']. ' '.$customers['city'].', '.$customers['state'].' '.$customers['zip']); ?></td>
<td><?php echo h($customers['email1']); ?></td>
<td><?php echo h($customers['phone1']); ?></td>
</tr>
<?php } ?>
Replace the url http://example.com/... with whatever real url you want to use for the link.

Related

How to hide table rows that fetch the info from a database based on string given and when a button is pressed

An HTML table is given in which there are people (agents in this case) who have the following information displayed Id, Name, Rank, Location, and Status. Status can have 3 values. Online, Offline, Disconnected. Based on the status that one of the agents has I want to put 3 buttons as such Online, Offline, and Disconnected, and when a button is pressed to hide the other rows with different values. For example, if I press Online, table rows on the HTML side that contain the status Offline and Disconnected disappear, this goes for the others too the other way around. I have no idea to achieve what I said earlier and I am open to any solution that is deemed to resolve my problem.
<table id="main_table">
<tr>
<td>Id</td>
<td>Agent Name</td>
<td>Agent Rank</td>
<td>Agent Location</td>
<td>Status</td>
</tr>
<?php
require 'CMS/processing/conn.php';
$result = mysqli_query($con,"SELECT * FROM agents");
while($info = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $info['id']; ?></td>
<td><?php echo $info['agentName']; ?></td>
<td><?php echo $info['agentRank']; ?></td>
<td><?php echo $info['locationNames']; ?></td>
<td><?php echo $info['agentStatus']; ?></td>
</tr>
<?php
}
?>
</table>
Try using the below logic : Create 3 buttons with <a> and pass status value as GET to the same page then while fetching check whether it has GET or not
Note:This is not a answer :passing direct GET value to sql query may cause some security issues
Online//put the status value based on your status
Offline//put the status value based on your status
Disconnected//put the status value based on your status
<table id="main_table">
<tr>
<td>Id</td>
<td>Agent Name</td>
<td>Agent Rank</td>
<td>Agent Location</td>
<td>Status</td>
</tr>
<?php
require 'CMS/processing/conn.php';
if(isset($_GET['status']))
{
$result = mysqli_query($con,"SELECT * FROM agents where agentStatus='".$_GET['status']."'");
}
else
{
$result = mysqli_query($con,"SELECT * FROM agents");
}
while($info = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $info['id']; ?></td>
<td><?php echo $info['agentName']; ?></td>
<td><?php echo $info['agentRank']; ?></td>
<td><?php echo $info['locationNames']; ?></td>
<td><?php echo $info['agentStatus']; ?></td>
</tr>
<?php
}
?>
</table>
I'd use javascript within a tag or jQuery since this sounds like you need dynamic functionality on a static webpage.
For your button pass in the name of the function you're creating inside your tag
<button onclick="yourFunctionName(yourString)"></button>
Inside your tag have the function accept the yourString paramater
function yourFunctionName(yourString){
if(yourString === "whatever"){
$('tbody tr').hide()
$('tbody tr').show()
}
etc! Let me know if you need any more clarification on anything
Output echo an attribute on each row <tr data-status="online"> etc.
Then in script use document.querySelectorAll("tr[data-status='online'] to find all rows with that status, loop thru the array and toggle each row style.display to 'table-row' or 'none' as required.
You could also do the same using CSS by assigning a class <tr class="online"> etc. Then in script dynamically altering the CSS definition of that class, changing the display to 'table-row' or 'none' as required.

how to add my database on a html table using session variables in php

I have a website that has multiple users that submit some requests, So each user will have his own Requests table that has been made. my issue is I'm trying to show the requests for every user in a table by using the session variables to get the name of the user and match it with the request table but it dose not work.
$userin= $_SESSION['username'];
$req = "SELECT * FROM request_table where r_status='pending' AND requester = 'max';";
$request_table = $conn->query($req);
it works if I out ( Max ) only but when I put it as a session variable or as $userin it dose not work and I get this error :
Notice: Trying to get property 'num_rows' of non-object in C:\xampp\htdocs\website\b_table.php on line 54.
Below is my code for the table :
<body>
<h1 align="center">Table</h1>
<table border="5" align="center" style="line-height:20px;">
<tr>
<th>Request ID</th>
<th>Requester name</th>
<th>Customer name</th>
<th>Description</th>
<th>Submition Date</th>
<th>Request Status</th>
<th>link</th>
</tr>
<?php
//Fetch Data form database
if($request_table->num_rows > 0){
while($view_request = $request_table->fetch_assoc()){
?>
<tr>
<td><?php echo $view_request['request_id']; ?></td>
<td><?php echo $view_request['requester']; ?></td>
<td><?php echo $view_request['customer_name']; ?></td>
<td><?php echo "Site A is:". $view_request['site_a']. ",<br> Site B is: ".$view_request['site_b']. ",<br>NO. Of links =:".$view_request['no_oflinks']; ?></td>
<td><?php echo $view_request['sub_date']; ?></td>
<td><?php echo $view_request['request_status']; ?></td>
<form action="view_request.php" method="GET"><td><button type="vieww" name="vieww" value= <?php echo $view_request['request_id']; ?>> View </button></td></form>
</tr>
<?php
}
}
else
{
?>
<tr>
<th colspan="7">No Data availabe</th>
</tr>
<?php
}
$conn->close();
?>
</table>
``````````````````````````````````````````````````````````````````````````````````````````
[enter image description here][1]
[1]: https://i.stack.imgur.com/pOHOX.jpg
you can do some debugging and testing.
like check if session variable is set or not like :
if(isset($_SESSION['username'])){
$userin= $_SESSION['username'];
}
else{
die("Session user name wasn't set");
}
and for the query
$req = "SELECT * FROM request_table where r_status='pending' AND requester = '$userin';"
for more debugging and testing , you can use vardump to check if data is present or not like : vardump($request_table);
hope it helps you in some way...
hope it helps.

Making table in PHP from database

I have a database called clanovi (members in englsih) which has stored name, surname, adress, email and gender of members. What I am trying to do is make a table from this data from database.
Here is my code https://jsfiddle.net/9j34xfwx/2/
<body>
<?php
require_once("Konekcija.php");
error_reporting(0);
mysql_connect("localhost", "root", "");
mysql_select_db("umv");
$sql=mysql_query("SELECT * FROM clanovi ORDER BY rbroj ASC");
$ime='ime';
$prezime='prezime';
$adresa='adresa';
$email='email';
$spol='spol';
?>
<table id='display'>
<?php
while ($rows=mysql_fetch_assoc($sql)){
?>
<tr><td>Ime clana: <?php echo $rows[$ime]; ?></td>
<tr><td>Prezime clana: <?php echo $rows[$prezime]; ?></td>
<tr><td>Adresa clana: <?php echo $rows[$adresa]; ?></td>
<tr><td>Email clana: <?php echo $rows[$email]; ?></td>
<tr><td>Spol clana: <?php echo $rows[$spol]; ?></td>
<?php
}
?>
</table>
Here is photo of my web page when it lists all members
from database http://pho.to/ACgD1
My question is, how can I make a table so members name, surname, adress, email and gender are in columns, and beneath are the data from database. As you can see on the photo, my code adds one member beneath another member.
You're creating a new row for every value (and then never closing those rows, which the browser is attempting to correct for you):
<tr><td>Ime clana: <?php echo $rows[$ime]; ?></td>
^--- here
Just create the row once for each iteration of the loop:
<tr>
<td>Ime clana: <?php echo $rows[$ime]; ?></td>
etc.
</tr>
I assume that you don't need to repeat the text like "Ime clana:" (say label) in each Iteration, and need to set it as column header instead, cause that's the exact problem that the HTML <table> is designed to solve. Also if you need the label data to be redundant, then you must be defeating the purpose of using a table, IMHO.
You need to do something like this..
<table id='display'>
<tr>
<th>Ime clana:</th>
<th>Prezime clana:</th>
<th>Adresa clana:</th>
<th>Email clana:</th>
<th>Spol clana:</th>
</tr>
<?php
while ($rows=mysql_fetch_assoc($sql)){
?>
<tr>
<td><?php echo $rows[$ime]; ?></td>
<td><?php echo $rows[$prezime]; ?></td>
<td><?php echo $rows[$adresa]; ?></td>
<td><?php echo $rows[$email]; ?></td>
<td><?php echo $rows[$spol]; ?></td></tr>
<?php
}
?>
</table>
Use this It's work for you.
<table id='display'>
<thead>
<tr>
<th>Ime clana:</th>
<th>Prezime clana:</th>
<th>Adresa clana:</th>
<th>Email clana:</th>
<th>Spol clana:</th>
</tr>
</thead>
<tbody>
<?php
while ($rows=mysql_fetch_assoc($sql)){
?>
<tr>
<td><?php echo $rows[$ime]; ?></td>
<td><?php echo $rows[$prezime]; ?></td>
<td><?php echo $rows[$adresa]; ?></td>
<td><?php echo $rows[$email]; ?></td>
<td><?php echo $rows[$spol]; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>

Display image slideshow by using php mysql

i have a database named hotel_booking,it contains field named imglinks which stores the link of the images and i am only able to display single image in the site. i want to display a slide show of the images of the respective hotels.
<tr>
<th>NAME OF HOTELS</th>
<th>CATEGORY</th>
<th>DESCRIPTION</th>
<th>ADDRESS</th>
<th>GALLERY</th>
</tr>
<?php
while($row=mysql_fetch_array($query))
{
$f1=$row['name'];
$f2=$row['category'];
$f3=$row['description'];
$f4=$row['address'];
?>
<tr>
<td><?php echo $f1 ?></td>
<td><?php echo $f2 ?></td>
<td><?php echo $f3 ?></td>
<td><?php echo $f4 ?></td>
<td><img src="<?php echo $row['imglinks'];?>" height="200" width="55"></td>
link to image of what i want to achieve
You can use bootstrap to achieve this easily with a few lines of code.
http://getbootstrap.com/javascript/#carousel
Go through this link and if there is any problem let me know :)

user ID and Dropdown sql php server side

the code for the view page:
<table id='display'>
<thead>
<tr>
<th>ID</th>
<th>NAME</th>
<th>DEPARTMENT</th>
<th>TELEFON</th>
<th>FAKS</th>
<th>E-MAIL</th>
<th>DATA</th>
<th>PURPOSE</th>
<th>ACTION</th>
</tr>
</thead>
<tbody>
<?php
$connect = mysql_connect("localhost","root","");
if (!$connect){
die(mysql_error());
}
mysql_select_db("permohonan_data");
$option = '';
$results = mysql_query("SELECT * FROM pemohon ORDER BY id DESC");
$option .='<select>';
while ($row = mysql_fetch_array($results)){
?>
<tr>
<td><?php echo $row['id']?></td>
<td><?php echo $row['name']?></td>
<td><?php echo $row['unit']?></td>
<td><?php echo $row['telefon']?></td>
<td><?php echo $row['faks']?></td>
<td><?php echo $row['email']?></td>
<td><?php echo $row['data']?></td>
<td><?php echo $row['purpose']?></td>
<td><?php echo $option .="<option value='" .$row['action']. "'>".$row['action']."</option>"?></td>
</tr>
<?php
}
$option .='</select>';
?>
</tbody>
</table>
the ouput:
the full detail contains id, name, position, department, institute, email, telefon and so on which doesnt show on the table because of lack of space. my question is, how when I clicked the ID number(auto increrment) of any user, I could see the full details of the user? like in a popup form?
and my dropdown for action column are empty, supposedly have 'approve' and 'disapprove' option but I didnt know how to insert that because its for admin to valid the thing, not the user. so I can't put the option on the client side where the user is. how can I create a new client side just for admin?
idk. please help. :(
change
<td><?php echo $row['id']?></td>
to
<td><?php echo "<a href='full_details.php?id=".$row['id']."'>".$row['id']."</a>" ?></td>
create a page called full_details.php and use the id to query the db
Using jQuery you can do that.
using a click event you can send id to a "full_details" page and show using a popup the data that page returns back.
See here: jquery.com
To use jquery you have include it on your page using tag and after that you can write your own function to receive click action on the id and after that you can send async request to your page and show the result without reload the page.

Categories