Displaying information from a database in a loop - php

On my website i currently have an events page. Each event is read and displayed using a database. for each row in the database i have created a new div like:
<body>
<?php
define('INCLUDE_CHECK',true);
require 'connect.php';
$result = mysql_query('SELECT * FROM test ORDER BY timestamp DESC LIMIT 3;') ?>
<?php while($row = mysql_fetch_assoc($result)) : ?>
<div class="aboutUsContenttest">
<div class="boxheader"><?php echo $row['a'] ?><br></div>
<div class="aboutUsContentText"> <?php echo $row['b'] ?></div>
</div>
<?php endwhile ?>
</body>
this displays 3 different events.....
what i want to do is have an option to "Read More" meaning that i could click on a link which takes me to a separate page with more information about each event. how would i achieve this for say the second event? how would it know to take the information from the second row or div which has been created?
i hope this makes some sort of sense
Thank you

Pass the parametr in the address:
$limit = isset($_GET['view_all']) ? "" : "LIMIT 3";
$result = mysql_query("SELECT * FROM test ORDER BY timestamp DESC {$limit};");

Just have a page named the value of $row['b'] with a php file extension, and create an anchor with the href set to '$row['b'].php'
or, you could add the name of the page as a field in your database.
<body>
<?php
define('INCLUDE_CHECK',true);
require 'connect.php';
$result = mysql_query('SELECT * FROM test ORDER BY timestamp DESC LIMIT 3;') ?>
<?php while($row = mysql_fetch_assoc($result)) : ?>
<div class="aboutUsContenttest">
<div class="boxheader"><?php echo $row['a'] ?><br></div>
<div class="aboutUsContentText"> <?php echo $row['b'] ?></div>
<div><?php echo 'Read More'?></div>
</div>
<?php endwhile ?>
</body>

Related

Inserting PHP database into HTML table

Hey I've recently been making a website and want to display the data from my database in a grid format opposed to it just listing down the page.
Here is my code right now:
<p>
<a href="pokemondetails.php?dex=<?php echo $row['dex'];?>">
<?php echo $row['name']; ?>
<br>
<img src="assets/<?php echo $row['dex']?>.png">
</a>
</p>
I was wondering how I would go about creating a for loop to allow the data from this database in conjunction with the image to span across the page with 7 columns and however many rows down until it reaches the end of the database.
Thanks!
<?php
$query = "Select * from tablename";
$bind = $conn->query($query);
if ($bind->num_rows > 0){
while ($row = $bind->fetch_assoc()){
?>
<p>
<a href="pokemondetails.php?dex=<?php echo $row['dex'];?>">
<?php echo $row['name']; ?>
<br>
<img src="assets/<?php echo $row['dex']?>.png">
</a>
</p>
<?php
}
}
?>
Try this, I just add while loop until End Of file (EOF table)

Displaying a PHP result

I am trying to learn php together with MYSQL. I have built the database on on the from end I have written the code below to bring in the website title from the database which works. However, I have two questions.
How would I get the result to display in <h1> tags?
Is this the cleanest way of pulling this value through?
Any help or advise greatly appreciated
<?php
include 'connection.php';
$sql = "SELECT VariableValue FROM VariableValues WHERE VariableID = '1'";
$result = $conn->query($sql);
while($header = $result->fetch_assoc()) {
echo $header["VariableValue"]. "<br>";
}
?>
To get a single row from db, you could use this:
$header_title = implode(mysqli_fetch_assoc(mysqli_query($conn, "SELECT VariableValue FROM VariableValues WHERE id = 1 LIMIT 1")));
Put a php variable between a html tag :
<tag><?php echo $var; ?></tag>
Eg :
<title><?php echo $header_title; ?></title>
You can echo all types of tags in PHP echo
for example:
echo '<div class="col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2">
<div id="success_alert" align="center" class="alert alert-success">
<strong>Log in to download this chapter.</strong>
</div>
</div>';
I have echoed different types of bootstrap tags in echo. Similarly you can also echo all types of tags you want.
To displays it in tags, use <h1><?php echo $var; ?></h1> for example.

How do I insert a horizontal rule after a set of PHP MySQL Select Queries?

I am currently working on developing a PHP website, and I would like to add a section to the home page that displays news headlines. The information to populate this section, like all other content on the site, will be pulled from a MySQL database. I have added the code that will let me do this and a little CSS before each part to format the display on the page and that works wonderfully. But, what I cannot work out is how to display all the content I want and insert a horizontal rule after each MySQL row is read. Right now, if I insert more than one row into the database table, it only displays the information from the new row and replaces the info from the previous. Refer to the image below to see how the page currently appears.
Below is the code that I am using.
<div id="news">
<?php
$query = "SELECT news_date FROM news";
$result = mysqli_query($dbconnect, $query);
while($row=mysqli_fetch_assoc($result)) {
$date = $row['news_date'];
}
?>
<?php
$query = "SELECT news_headline FROM news";
$result = mysqli_query($dbconnect, $query);
while($row=mysqli_fetch_assoc($result)) {
$headline = $row['news_headline'];
}
?>
<?php
$query = "SELECT news_body FROM news";
$result = mysqli_query($dbconnect, $query);
while($row=mysqli_fetch_assoc($result)) {
$body = $row['news_body'];
}
?>
<div class="date"><?php echo $date . "<br />" ?>
<div class="headline"><?php echo $headline . "<br />" ?>
<div class="headlinebody"><?php echo $body . "<hr />" ?>
</div>
As I've said, this will display the info the way I want it, but as soon as I add another "headline", the new replaces the old. I would like for all headlines to appear with a horizontal rule between the headlines (rows) and not each part. Any help you can provide would be appreciated. Thank you much in advance.
You can do this in 1 query and 1 loop.
<div id="news">
<?php
$query = "SELECT news_date, news_headline, news_body FROM news";
$result = mysqli_query($dbconnect, $query);
while($row=mysqli_fetch_assoc($result)) { ?>
<div class="date"><?php echo $row['news_date']; ?></div><br />
<div class="headline"><?php echo $row['news_headline']; ?></div><br />
<div class="headlinebody"><?php echo $row['news_body']; ?></div><hr />
<?php }
?>
</div>

Issue with bootstrap grid and mysql response

I have an issue with bootstrap and creating a 4 column responsive grid from a mysql response.
The problem is that if the second mysql query has a variable number of results, it brakes the grid.
Here is my code (where the first query has 9 results and the second query has a variable number of results):
<?php
$a = "SELECT * FROM $table_users ORDER BY username";
$result = mysql_query($a);
?>
<div class="container">
<div class="row">
<?php while ($row = mysql_fetch_array($result)) {?>
<div class="col-xs-3" style="background-color:aqua;">
<?php echo $row['username'];
$b = "SELECT * FROM $table_presents WHERE bought_for='$row[username]' OR bought_for='' ORDER BY id";
$result_presents = mysql_query($b) or die(mysql_error());
while ($row_presents = mysql_fetch_array($result_presents)) {
?>
<div style="background-color:red;">
Hello world!
</div>
<?php }?>
</div>
<?php }?>
</div>
</div>
which gives me this:
enter image description here
instead of this (obviously with many 'Hello world'):
enter image description here
Any help greatly appreciated!
Bootstrap doesn't claim to do any kind of elegant bin-packing on panels with different sizes. You could do some programming or css work to make all your panels the same size.
If that doesn't work for your application, you're going to need a layout library that does bin-packing so these panels of different sizes align properly.
There are a few such libraries available as jQuery plugins.
In this, $row[username] is wrong as it should be $row['username'].
$b = "SELECT * FROM $table_presents WHERE bought_for='$row[username]' OR bought_for='' ORDER BY id";
BTW, I changed your code bit. Please Try this.
<?php
$a = "SELECT * FROM $table_users ORDER BY username";
$result = mysql_query($a);
?>
<div class="container">
<div class="row">
<?php while ($row = mysql_fetch_array($result))
{
$username=$row['username'];
?>
<div class="col-xs-3" style="background-color:aqua;">
<?php echo $username;
$b = "SELECT * FROM $table_presents WHERE bought_for='$username' OR bought_for='' ORDER BY id";
$result_presents = mysql_query($b) or die(mysql_error());
while ($row_presents = mysql_fetch_array($result_presents)) {
?>
<div style="background-color:red;">
Hello world!
</div>
<?php }?>
</div>
<?php }?>
</div>
</div>
[NOTE: Users can inject your SQL commands. Use prepared statements and parameterized queries. For more info, click Prevent SQL Injections

Output Name in PHP From mysql Database

I have a list of names in a database that i want to display one by one
(also for bonus points, another column in the database is a Boolean value for if a task is completed or not. if this is true i want the css content box background to be green instead of red.)
so how can i select a name from row one, put it to a PHP variable, then select the value from the "Name" column in row 2 and put that to another PHP variable or the next item in the array?
thanks for any help!
<html>
<head>
<title>Title</title>
<link rel="stylesheet" type="text/css" href="mngPWinCSS.css"/>
</head>
<body>
<?php
$dsn ='mysql:host=****.******.com;dbname=o****_**n';
$username='********';
$password ='******';
mysql_connect('localhost',$username,$password);
$query=mysql_query("SELECT Name FROM CLOAS_Team LIMIT 0,1");
$bob="dkajfk";
$url=$_SERVER['REQUEST_URI'];
header("Refresh: 60; URL=$url");
$com[1]="i";
$com[2]="i";
$com[3]="i";
$com[4]="i";
$com[5]="i";
$com[6]="i";
$name=mysql_fetch_array($query);
?>
<div id="content">
<img src="logjpg.JPG" alt="Smiley face" height="50" width="200">
<h3>CLOAS Tracker</h3>
</div>
<div id="Content">
<?php
?>
<div id="complete">
<h3names>
<?php
echo $name['Name'];
?>
</h3names>
</div>
<div id="incomplete">
<h3names>Name2</h3names>
</div>
</div>
</body>
</html>
First you need to change your SELECT query to select all of the rows that you wish to display, perhaps by taking off the LIMIT clause. Something like this;
$result=mysql_query("SELECT Name FROM CLOAS_Team");
(This will get you all of the names in your table.)
Next, you need to loop through the results you got from this query, like so;
$names = array();
while($row = mysql_fetch_assoc($result))
{
$names[] = $row['Name'];
}
This will put them into the array $names for you, which you can then work with. Instead of putting them into the array, you might want to output them immediately, perhaps like this;
while($row = mysql_fetch_assoc($result))
{ ?>
<div>
<h3>
<?php
echo $row['Name'];
?>
</h3>
</div>
<?php } ?>
However, you have many more errors in your code. Such as;
You can't just invent html elements called <h3names>
I doubt that you want to set the id attribute to 'incomplete'. An id should be unique, I expect you should be putting this in as a class (class = "incomplete")
I don't think your line header("Refresh: 60; URL=$url"); will do anything as your headers have already been sent to the page. If you want this line to work, it needs to be right at the top, BEFORE any output has been sent to the browser.
And for the bonus point, include the 'Completed' field in your query (if that is what it is called) and use this to add a style to each <div> element that you display in your loop. So your query might become;
$result=mysql_query("SELECT Name, Completed FROM CLOAS_Team");
And your loop would now be like this;
while($row = mysql_fetch_assoc($result))
{ ?>
<div style = "background-color:<?php echo $row['Completed'] == true ? 'green' : ' red'; ?>">
<h3>
<?php
echo $row['Name'];
?>
</h3>
</div>
<?php } ?>

Categories