I'm trying to display more details of the data when the table row <tr> is clicked. But I have no idea how to implement it. Here's my code
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "hotel_dir";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT hotel_img, hotel_name, hotel_address, hotel_phone FROM hotel";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td><img src=".$row["hotel_img"].
"></td><td>".$row["hotel_name"].
"<br />".$row["hotel_address"].
"</td><td>".$row["hotel_phone"].
"</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
Here's the image.
hotel list
The problem is, I'm trying to pass it to hotel.php showing more details of the hotel when the table row is clicked.
You must have an Id field in your hotel table you can include it and have an anchor tag to link to other page with this id,
actually some thing like this:
For SQL statement:
for SQL statement you select id and the previous fields that you had selected already, as usually id is a unique column in tables you can get detail of one row and show all detail of specied id.
Here in this page your sql statement should change to :
$sql = "SELECT id,hotel_img, hotel_name, hotel_address, hotel_phone FROM hotel";
and in the detail.php page your SQL statement must be something like this(of course I didn't consider security stuff):
$sql = "SELECT * FROM hotel where id=".$_GET['id'];
for your table :
Here I just added a tag call detail.php file with parameter id that you can get it in details.php for show details
echo "<tr>
<td><a href="http://yoursite.dev/detail.php?id=".$row["id"]>See Details</a></td>
<td><img src=".$row["hotel_img"]."></td>
<td>".$row["hotel_name"].
"<br />".$row["hotel_address"].
"
</td>
<td>".$row["hotel_phone"].
"
</td>
</tr>";
And in your detail.php file you get parameter id with $_GET['id'] and show more detail for specified id
Related
I have a database were the information from the database comes on to my website (code below).
On another page I have a text box were you type in a new table, it creates a new table and sends the table name to another table which stores all the tables. I want to view all the tables from that table list but have them when i click on them take me to view the other tables.
I have very little experience with databases and PHP but I'm thinking if I can store the table variable in cookies and then initiate it in another document and have it resets every time the user returns to the page which has all of the other tables they can view.
But I still don't know how to create links to that other doc which runs all the other tables.
This text below is in a file called db_results.php
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM MyGuests ORDER BY id DESC LIMIT 500";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<p>". $row["firstname"]. " " . $row["lastname"] . "</p>";
}
} else {
echo "0 results";
}
$conn->close();
?>
this code views the results and is just called index.php
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
setInterval(function() {
$("#load_results").load("db_results.php");
}, 1000);
});
</script>
<div id = "load_results"></div>
the code is not setup for links yet but the links would go into last name and the viable name into first. please message me for any further questions.
You will have to change the echo to echo a link.
If it really goes into your last name field it would look something like this:
echo "<p>". $row["firstname"]. " " . $row["lastname"] . "</p>";
But it wouldbe better if you created another field in your table to insert the link into.
i want to take the result of an sql query "user_id"and search using it in
in another query in another table
for example :
main query select * from tracker
second query: take the id from the first query to replace it with the name of the user found in another table "user table".
something like :
select user_name from vtiger_users where id = $row["id"]
here is my code below
<?php
$servername = "localhost";
$username = "x";
$password = "xyz";
$dbname = "dbname";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, module, whodid,changedon FROM vtiger_modtracker_basic";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table style='float: left'><tr><th>ID</th><th>Module</th><th>Who Did</th><th>Time Of Action</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
$user_id=$row["id"];
$username_sql="SELECT user_name FROM vtiger_users where id=".$user_id." ";
$result_username=$conn->query($username_sql);
$row2 = $result_username->fetch_assoc();
echo "<tr><td>".$row["id"]."</td><td>".$row["module"]."</td><td>".$row2["user_name"]."</td><td>".$row["changedon"]."</td></tr>";
//echo "<p>".$row2["user_name"]."</p>"
//echo $row2["user_name"];
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
I guess you should use whodid column when you set $user_id = $row['whodid'] or you can use JOIN query. It will return you user_name from vtiger_users table.
SELECT
a.id,
a.module,
a.whodid,
a.changedon
b.user_name
FROM vtiger_modtracker_basic a
JOIN vtiger_users b ON a.whodid = b.id
thanks all ,that query did the job for me
SELECT vtiger_modtracker_basic.id, vtiger_modtracker_basic.module, vtiger_modtracker_basic.whodid ,vtiger_modtracker_basic.changedon ,vtiger_users.user_name FROM vtiger_modtracker_basic ,vtiger_users where vtiger_modtracker_basic.whodid = vtiger_users.id
I've just started learning PHP so this is quite a struggle for me, any help is appreciated!
So, I've managed to select my database, table and get data from it. But I need to select more data from the older columns.
Here is some of the code:
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//execute the SQL query and return records
$result = mysqli_query($conn, "SELECT username FROM interview");
while($rowval = mysqli_fetch_array($result))
{
$username= $rowval['username'];
$username2= $rowval['username'];
$username3= $rowval['username'];
$username4= $rowval['username'];
}
?>
And I am displaying the data in a button, I want $username to display the first column and $username2 to display the second column etc...
I fetch the data using this code:
<?php echo $username; ?>
<?php echo $username2; ?>
<?php echo $username3; ?>
<?php echo $username4; ?>
Thanks!
I would suggest you re-factoring your "while" loop the following way for better scalability:
$usernames=array();
while($rowval = mysqli_fetch_array($result))
{
$usernames[] = $rowval['username'];
}
Then, you'll be able to access usernames like:
echo 'The first user name is: '.$usernames[0];
echo 'The second user name is: '.$usernames[1];
echo 'The third user name is: '.$usernames[2];
My php skills are a bit rusty, so take this with a grain of salt:
At first it is important to get the language right. A database table consists of rows and columns, the columns are the ones you select with the the part after the SELECT statement, if you write SELECT username what you select is the username column.
SELECT username FROM interview;
So if you want to select more columns from your table you have to put them in your SELECT statement. Let's say you want to select the username and the status of the interview, you would need to include the status in your SELECT statement like this:
SELECT username, status from interview;
Then you can access the interview status just like you did it with the username.
$result = mysqli_query($conn, "SELECT username, status from interview;");
while($rowval = mysqli_fetch_array($result))
{
$username= $rowval['username'];
$status = $rowval['status'];
}
Remember that what you get as the $rowVal is just a single row of your table. So if you want to collect all usernames and statuses from your table you have to use a datastructure to collect them.
$queryResult = mysqli_query($conn, "SELECT username, status from interview;");
$result = array();
while($rowval = mysqli_fetch_array($queryResult))
{
$username= $rowval['username'];
$status = $rowval['status'];
// With this action we build an array, that has each row's result as value.
$result[] = array('username' => $username, 'status' => $status);
}
To access the results you can then query the $result array like this:
// The interview status of the first entry in your table
$result[0]['status'];
I have a form constantly receiving input from users,
I need to keep displaying the next entry comment and only that comment.
I have id, Name and Comment in my database table 'demo'
id Name Comment
1 john hello
2 may yo
3 dereck all the best
How to display 1 entry only then after 5 mins, next entry and repeat?
Viewing.php:
<?php
//connection to
require 'config.php';
$conn = mysqli_connect( $db_host, $db_username, $db_password, $db_name);
// Create connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//$sql = "SELECT * FROM `demo` ORDER BY `id` DESC ";
$sql = "SELECT id, Name, Comment FROM demo";
$result = $conn->query($sql);s
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<table border='1'>";
echo "<tr><td height='150px'></td></tr>";
echo "<tr><td width='30%'> <td>";
echo "<td><div id='colcenter' style='background:url(viewBG.png);width:700px;height:650px;'>".$row["Comment"]."<br>From : ".$row["Name"]."</div></td>";
echo "<td width='30%'> <td>";
echo "<tr><td height='150px'></td></tr>";
echo "</table>";
}
} else {
echo "0 results";
}
$conn->close();
?>
I think there is a better way.Use max():
SELECT Max(id) as MaxId FROM `DEMO`
Edit
first imagine it's your .html or your .php file that contains a button:
<script>
function Show(){
$.post("example.php",{method:'show'},function(data){
$("#entry").html(data)
});
}
</script>
<div id="entry">entries will be shown here</div>
<input type="button" value="Show me new entries" onClick="Show ();" />
and then in example.php you can echo the last entry.I think you know what you should write in example.php but let's give you a sample:
<?php
if(isset($_POST['method']) and trim($_POST['method'])==='show'){
//echo the last entries
}
?>
You could use the id, if it's autoincrement, to get the latest post. Do:
SELECT * FROM DEMO ORDER BY id DESC LIMIT 1
That will give you the entry with the greatest id (= the entry last inserted).
I want to display 30 rows from an data base,on an TABLE, but just the approved entries.
This "approved entries" are defined for an row with values "1 for approved" and "0 for NOT approved"
This is the code wath I need to change:
<?php
$servername = "localhost";
$username = "username ";
$password = "password";
$dbname = "name";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, label, title_id, season, episode, approved FROM links order by id desc LIMIT 30 OFFSET 1";
$result = $last_id = $conn->query($sql);
if ($result->num_rows > 0)
{
echo "<table><tr><th>ID</th><th>Audio</th><th>URL</th><th>Temporada</th><th>Episodio</th><th>Aprobado</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["id"]."</td><td>".$row["label"]."</td>";
echo "<td><a href=";
if (empty($row['episode'])) {
echo '/peliculas-online/'.$row["title_id"];
}
else {
echo '/series-online/'.$row['title_id'].'/seasons/'.$row['season'].'/episodes/'.$row["episode"];
}
echo ">".$row["title_id"]."</a></td>";
echo "<td>".$row["season"]."</td><td>".$row["episode"]."</td><td>".$row["approved"]."</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
Like you can see, this code list all (approved or not)... how to get something like this:
if row approved=1 display rows, else hide (hide all not just the approved row)
Need to HIDE all entries from this table row if entry is not approved -> id-label-title_id-season-episode-approved
Thanks
You should know about the MySQL WHERE clause in SELECT.
Something like:
"SELECT id, label, title_id, season, episode, approved
FROM links
WHERE approved = 1
ORDER BY id desc
LIMIT 30 OFFSET 1";
Reference:
https://dev.mysql.com/doc/refman/5.0/en/select.html