Change content of div depending on user input? - php

I have created a table of people within my database using the following code:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT clientname, clientsurname, address1, postcode, dob FROM clients";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table class='table table-bordered'><tr><th>Name</th> <th>Surname</th><th>Address</th><th>Postcode</th><th>Date of Birth</th><th>Select Client</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["clientname"]."</td><td>".$row["clientsurname"]."</td><td>".$row["address1"]."</td><td>".$row["postcode"]."</td><td>".$row["dob"]. "</td><td class='btn btn-block btn-info btn-flat'><a href=\"edit_form.php?id=$row[clientname]\" style='color:#fff;'>Select</a></td></tr>";
}
echo "</table>";
} else {
echo "There are 0 clients in the system matching your search criteria";
}
$conn->close();
?>
I want to know if it is possible that when a user clicks the Select Client button if that clients full data can populate a div to the side of the table?
If this is possible how would I do this?

Try using the .fadeToggle or .fadeTo in jquery to make it appear on the side of the screen. This can be used many different ways but for the most part this is what it looks like.
$(document).ready(function(){
$('button').click(function(){
$('h2').fadeTo('fast',1);
});
});
example: http://jsfiddle.net/JoshuaHurlburt/x5f3bz37/

Related

Show records based on a catergory value

I have a table with data in it ID, Name, Task, Business. I am trying to show all records based on the Business category.
I would like a separate page to show all the businesses and then when you click on the name it displays all the records associated with it.
I am unsure of how to display this.
I can almost see what I want in SQL workbench but I am unsure how to do this is php code..
SELECT * FROM job2 where business="MR Plumber"
I want the "Mr plumber" to be what ever business I click on the business page.
I would like to have a page to list the businesses and then when I click on the business name link and displays all records associated to it.
Try this.
<?php $servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
echo "error";
}
$sql = "your sql";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["column_name"];
}
} else {
echo "0 results";
}
$conn->close();
?>

PHP Looping table contacts and creating multiple files

I have some code where I access the database and fetch all the data from a table called snippets.
Everything works fine except I now need to create a new html file and save it with the contents of a field called code for each.
So basically each "code" field is saved as a new html file.
Here is the current code:
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT code FROM snippets";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
//save each code record as a new html page
}
} else {
echo "0 results";
}
$conn->close();
How can I do that?
Try this...
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
// if you have id field in your record then try this way
file_put_contents($row['id'].".html", $row); // 1 full path with name 2 your data
}
} else {
echo "0 results";
}

background refresh information from database AJAX

I have created a website which retrieves text which has been uploaded to my database. the problem with this is i want a refresh of the content from that database ever second. but i can't find how to do this anywhere. i want this done in the background. and if possible a way to make the textbook that i will put in later to input the data into the server, also not do a full refresh but send the content in the background. thank you every answer will count.
<?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 ASC LIMIT 10";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<div id='message'> <br> ". $row["firstname"]. " " . $row["lastname"] . "<br> </div>";
}
} else {
echo "0 results";
}
$conn->close();
?>
In a file, simply echo out the results. Then in another file, load the echoed results in a div like this:
db_results.php file which will echo out results:
<?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 ASC LIMIT 10";
$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();
?>
display.php file which will refresh the results in a div:
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
setInterval(function() {
$("#load_results").load("db_results.php");
}, 1000);
});
</script>
</head>
<body>
<div id = "load_results"></div>
</body>
</html>
db_results.php file will query database, will fetch the results and will echo them out. The second file (display.php) loads the first file in a div and refreshes the div every second, so updated results are loaded in the concerned div.
P.S.: Keep both files in same directory or adjust the path accordingly.

Getting User Data Based on Their Information

This first field is where a web visitor will enter in the 'cardname' hit submit and be directed to another page (dashboard2.php) where only his or her content will appear.
Enter your cardname to access your content<br>
<form action='dashboard2.php'>
<input type='text' name='cardname'/><input type='submit' value='retrieve card'/>
</form>
</body>
The page below is the page that is directed after the user enters in the 'cardname' from the first input field. However, I only want this second page to show the information based on the cardname that was entered. Right now, it shows every single cardname, questionone, answerone from that table.
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "flashcards";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT cardname, questionone, answerone FROM cards";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> ". $row["cardname"]. " ". $row["questionone"]. " " . $row["answerone"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
You have to modify the query to accept a WHERE clause. For instance, WHERE cardname = mysqli_real_escape_string($conn, $_GET['cardname']) (The default method for any form is GET unless you specify method="post".).
You should learn about prepared statements for MySQLi and perhaps consider using PDO, it's really not hard.
It seems that you want to perform a search and not a display all the records.
Usually a search returns records that match a certain field, unless a specific ID or unique value was entered in the search. I'm not sure this is the case.
I put this together a little quick but hopefully it helps...
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "flashcards";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// escape the string to avoid SQL injections
$searchEscaped = $conn->real_escape_string($_POST['cardname']);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT cardname, questionone, answerone FROM cards WHERE cardname = '$searchEscaped' ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
if($result->num_rows == 1){
// only one result found, show just that
$row = $result->fetch_assoc()
echo "<br> ". $row["cardname"]. " ". $row["questionone"]. " " . $row["answerone"] . "<br>";
}else{
// multiple rows found, show them all
while($row = $result->fetch_assoc()) {
echo "<br> ". $row["cardname"]. " ". $row["questionone"]. " " . $row["answerone"] . "<br>";
}
}
} else {
echo "0 results";
}
$conn->close();
?>

Dynamically changing certain values of url using PHP

This is what my url is: north.php?slot=0
What I need to do is change the value of slot when I click on a div which I have it functioning as an arrow. In the div I am using:
onclick="location.assign('north.php?slot=1')".
The div is like this:
<a onclick="location.assign('north.php?slot=1')"><div class="arrow"></div></a>.
I am receiving the the value from url as:
<?php
$slot=$_GET['slot'];
?>
which is in head tag. Now I want to use this value to access content from my database and display it
I am doing it on xampp server. I have a column named id in my table.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "tourism portal";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
$sql="SELECT * FROM north where id=$slot";
$result = $conn->query($sql);
while($row = $result->fetch_assoc())
{
echo $row['name'].'<br>'.'<br>'.$row['description'];
}
?>

Categories