JSFiddle here, with unfunctionaing PHP obviously, but on the local host it pulls through the information just fine.
I am trying to append data in a loop into a specific set of tags, inside a di of a certain class.
For example, for every row in the the table, add a div with the class "card" and append "Title" into a H2, "description" into a P tag etc.
Anyone know how I could go about doing this? I'm googling but finding it hard to phrase my question right.
Div "Card" markup:
<div class="card">
<h2>Health and Wellbeing</h2>
<p>Sometimes you just did too much sitting not enough jumping go go go.</p>
</div>
PHP pull:
$sql = "SELECT title, description, count FROM relevant_topics";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<b>Title: </b>" . $row["title"]. " - Description: " . $row["description"]. " " . $row["count"]. "<br>";
}
} else {
echo "0 results";
}
You can echo the div markup into the PHP code like this:
while($row = $result->fetch_assoc()) {
echo'<div class="card">
<h2>'.$row["title"].'</h2>
<p>'.$row["description"].'
</div>';
}
Not sure where you want the Row['count'] though.
Also you might wanna use PDO instead of mysql, Since mysql is outdated: How can I prevent SQL injection in PHP?
check this
if ($result->num_rows > 0) {
// output data of each row
while($row = $result-> mysql_fetch_array()) {//if not works change mysql_fetch_array() to fetch_assoc()
echo'<div class="card"><h2>';
echo '.$row["title"].';
echo '</h2><p>';
echo '.$row["description"].';
echo '</P></div>';
}
Related
I'm loading tr and td elements into a table from a database using PHP, but when the table is loading, it jumps from the top of the page to the bottom of the page repeatedly until all the table elements are loaded. Is there any way for the page to stay in the same place without jumping to the table when the table is loading?
$result = $connection->query($query);
if ($result) {
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td> data from table</td>";
...
echo "</tr>";
}
}
}
EDIT: I put a lazy load on the page for now since it seems like there's no other solution.
You can try to store your html in a variable and echo it when the fetching is done.
$html = "";
$result = $connection->query($query);
if ($result) {
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$html .= "<tr>";
$html .= "<td> data from table</td>";
...
$html .= "</tr>";
}
}
}
echo $html;
If your elements loaded from DB are used to build sth. like images, then this could lead to the problem because the browser does not know the size of the elements in advance. You can try to set the dimensions of the elements or a kind of a container as placeholder in advance.
Perhaps this approach is helpful, too: Stop page jumping while images load
hey guys this maybe a stupid question for you but I have a database containing footballers names (please see screen shot) What i need to do is display maybe 5 names side by side and then 5 below, if that make sense. is there an easy way to do this?
the code i currently have after the established connect is
$sql = "SELECT ID, NAME FROM players";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
// Outputting HTML and the data from the DB. Change to $row['the name of the field you want']
echo "<div class='caption'><h3><img src='http://placehold.it/180x180' alt=''>" . $row['NAME'] . "</h3><p>";
}
} else {
echo "No players to show";
}
$conn->close();
?>
is it CSS i should be focusing on to make this look the way i want it?
sorry i am a new at this and cant get my head around it lol
try this:
I have added a counter called i which will change the div after it has listed 5 names. Then I have styled the div to 50% width and float to left which allows 2 div side by side.
<style>
.caption
{
width:50%;
float:left;
}
<?php
$sql = "SELECT ID, NAME FROM players";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
echo "<div class='caption'>";
while($row = $result->fetch_assoc()) {
$i = 0;
if($i>=4)
{
$i = 0;
echo "</div><div class='caption'>";
// Outputting HTML and the data from the DB. Change to $row['the name of the field you want']
<h3><img src='http://placehold.it/180x180' alt=''><h3>" . $row['NAME'] . "<h3>";
}
} else {
echo "No players to show";
}
$conn->close();
?>
</div>
yes, you are doing it right way. You just need to do some css. You can take some help form here :- https://designshack.net/articles/css/5-simple-and-practical-css-list-styles-you-can-copy-and-paste/
So I've written some PHP/SQL code and that side of things works. When the code retrives a new article however, I want it to also create a div called content, for which I have created a CSS class. I was reccomended to do this elsewhere as the following:
$mydiv = '<div name="content">';
$mynew = '</div>';
if ($result->num_rows > 0) {
//Output data of each row
while($row = $result->fetch_assoc()) {
echo $mydiv . "<h1>Title: " . $row["title"] . " <br /></h1><h2>Summary: " . $row["summary"] . " <br /></h2><h5>Author: " . $row["author"] . " </div><br />" . $mynew ;
}
} else {
echo "Cannot find content.";
}
?>
Dreamweaver seems to think there's no syntax problems, but when I launch the page on my server a new div is not created, and the text/background doesn't change in accordance with the class?
Kind of new here but seems like a great community, any help would be awesome!
You might find it easier while you are still learning, to de complicate the string concatenation, so it is easier to follow.
Also you need to be careful to create well formed HTML i.e. open and close tags in the right order i.e. the <h5> tag
Also to remove some of the start and stop in the string literal building, if you wrap an array referenced variable in {} you can use it in a double quoted string just like you would a scalar variable like $title
And finally, make sure that you can see the message saying there is no data in the result set from the database by adding[temporarily] a big highlight to it.
// just a test
echo '<div style="font-size:20px; color:red">Rows to process: = ';
echo $result->num_rows;
echo '</div>';
if ($result->num_rows > 0) {
//Output data of each row
while($row = $result->fetch_assoc()) {
echo '<div name="content">';
echo "<h1>Title: {$row['title']}<br /></h1>";
echo "<h2>Summary: {$row['summary']}<br /></h2>";
echo "<h5>Author: {$row['author']} </h5><br />";
echo '<div>';
}
} else {
echo '<div style="font-size:20px; color:red">Cannot find content.</div>';
}
I am learning php and made a simple application to save peoples names to a database then pull all the saved names into a list lower down on the page.
this is the code that is getting the names: echo "<div class=\"results\"> $row[Name] </div>";
I put a div with the class of results so that I could style it. At this point all I wanted to do was center the results. After styling it with css to be centered, looking at the page source each name that was echo'ed has a div around it with the class of results. They are all centered like I wanted which is good but this does not seem like the best way to do it because there will be a new div created for every new name in the database (right now there are 18 divs. Way too many!).I was hoping only one div would be created but I was obviously wrong about that.
So, is it possible to pre-create a div in the html markup with nothing in and echo the names into the div? Or do you think there would be a way to make it all output at once so it was all within the one div?
Here is the rest of the code so you get an idea of whats happening:
<?php
$connect = mysql_connect("localhost","user","0123");
if(!$connect){
die("Failed to connect: " . mysql_error());
}
if(!mysql_select_db("Test")) {
die("Failed to select database: " . mysql_error());
}
$results = mysql_query("SELECT * FROM Friends");
while($row = mysql_fetch_array($results)){
echo "<div class=\"results\"> $row[Name] </div>";
}
if ($_POST[name] !="") { $sql="INSERT INTO Friends
(name) VALUES('$_POST[name]')";
if (!mysql_query($sql,$connect))
{
die('Error: ' . mysql_error());
}
echo "<div id=\"added\"> $_POST[name] added to the database </div>";
}
else die("")
?>
Since you output the div inside your while loop, you will get a new div for each result. Just move the div outside the loop:
echo "<div class=\"results\">";
while($row = mysql_fetch_array($results)){
echo $row['Name'];
}
echo "</div>";
Note that you should always quote your keys. Use $row['Name'] instead of $row[Name] (obviously not if Name is a defined constant).
i think it's better to have some variable to store the output:
$out .= "<div class=\"results\">";
while($row = mysql_fetch_array($results)){
$out .= $row[Name]."<br />";
}
$out .= "</div>";
echo $out;
Don't let php output html tags. It looks bad in code and your editor will not recognise it as html and make a big mess in colouring it. Generally I would advise to do all your php processing first and then make the HTML with pieces of php output in it. Something like this:
Note that I replaced your div with an ul, as your list of persons seems better suited as an ul than a div. Obviously you can use any tag you'd like in the HTML
<?php
// do all php stuff here
$persons = /* get collection from database here */
?>
<html>
<head>
</head>
<body>
<ul id="ListOfPersons">
<? foreach ($persons as $person): ?>
<li>
<?= $person["Name"] ?>
</li>
<? endforeach ?>
</ul>
</body>
</html>
You can separate the div creation from the results echoing :
echo '<div class="results">';
while($row = mysql_fetch_array($results)){
echo $row['Name'].'<br/>';
}
echo '</div>';
and if you don't want the div if there are no results (no friends) you can use mysql_num_rows
Thank you for reading my question.
I am trying to make a site where information from a database is displayed onto a webpage. The end result will look like this, but for a different game.
Here is a plain HTML page of what I want it to look like.
So far I know that my connection to the database works. When I run:
mysql_select_db("DATABASE", $con);
$result = mysql_query("SELECT * FROM DATABASE");
while($row = mysql_fetch_array($result)) {
echo $row['Title'] . " " . $row['Type'];
echo "<br />";
}
It returns the Title and Type.
What I want to do is run an If/Else statement that runs a different that block of code depending on the card type.
while($row = mysql_fetch_array($result)) {
if ($row['Title'] == 'Hero') {
echo "<div>";
}
}
I tried this based on the tutorials at w3schools.com but it doesn't work.
Do any of you have any ideas for what I should do?
EDIT:
Here is what I tried running:
while($row = mysql_fetch_assoc($result)) {
if ($row['Title'] == 'Hero') {
echo $row['Title'] . " Hero.<br>";
} else {
echo $row['Title'] . " Who cares.<br>";
}
}
Here is the output (Gimli should show up as a Hero):
For Gondor! Who cares.<br>
Bilbo Baggins Who cares.<br>
Ungoliant's Spwan Who cares.<br>
Gimli Who cares.
EDIT 2: Thank you Phil for spotting the error, I now get the result I wanted using Mikushi's method. Thank you all so much.
The fetching of your mysql result seems wrong, should be like this:
while($row = mysql_fetch_assoc($result)) {
if ($row['Title'] == 'Hero') {
echo ""; }
}
mysql_fetch_array fetch the result as an indexed array (1=> data, 2=> thing) , which explains why $row['Title'] doesn't work.
The difference:
http://ca2.php.net/mysql_fetch_array
http://ca2.php.net/mysql_fetch_assoc
Please, always refer to the documentation, it's very well done and a better source than w3cschools.
Maybe it's one of those all too obvious things but...
Shouldn't it be
if ($row['Type'] == 'Hero') // "Type", not "Title"