Using echo with lists in php? - php

I have the following PHP code:
$getnews = mysql_query("SELECT * FROM news ORDER BY id DESC") or die(mysql_error());
while ($row = mysql_fetch_assoc($getnews)) {
$id = $row['id'];
$title = $row['title'];
$body = $row['body'];
$date = $row['date'];
echo "<div class=\"title\">$title</div><br>";
echo nl2br($body);
echo "<br><div class=\"date_time\">".time_ago($date)."</div>";
echo "<hr>";
}
This is used to create a news feed and I use echo to print out what is within the updates.
Is there a way in which I could maybe use a list to print out each update instead of the way im currently doing it?
Or is it possible to create a div around each update that the while loop creates?
I'm sorry if the question is not clear but thanks for all the help!
My newsfeed creates updates in a news feed like twitter. Each update is printed out using echo and surrounded by . Im trying to find a way in which I can create a list or div for the entire layout of each update. Im finding very difficult to arrange whats going on in each update.

The following code should get you there. Just echo out the html as you would like it to appear.
$getnews = mysql_query("SELECT * FROM news ORDER BY id DESC") or die(mysql_error());
while ($row = mysql_fetch_assoc($getnews)) {
$id = $row['id'];
$title = $row['title'];
$body = $row['body'];
$date = $row['date'];
echo "<div class='news-article'>";
echo "<div class=\"title\">$title</div><br>";
echo nl2br($body);
echo "<br><div class=\"date_time\">".time_ago($date)."</div>";
echo "</div>";
echo "<hr>";
}
A lot of the time a lot of echo statements like that just confuse what you are trying to do.
$getnews = mysql_query("SELECT * FROM news ORDER BY id DESC") or die(mysql_error());
while ($row = mysql_fetch_assoc($getnews)) {
$id = $row['id'];
$title = $row['title'];
$body = $row['body'];
$date = $row['date'];
?>
<div class='news-article'>
<div class="title"><?php echo $title ?></div><br>
<?php echo nl2br($body); ?>
<br><div class="date_time"><?php echo(time_ago($date)) ?></div>
</div>
<hr>
<?php
}
If you wanted to accomplish the same thing as an unordered list you would do this.
$getnews = mysql_query("SELECT * FROM news ORDER BY id DESC") or die(mysql_error());
echo "<ul class='article-list'>";
while ($row = mysql_fetch_assoc($getnews)) {
$id = $row['id'];
$title = $row['title'];
$body = $row['body'];
$date = $row['date'];
?>
<li class='news-article'>
<div class="title">$title</div><br>
<?php echo nl2br($body); ?>
<br><div class="date_time"><?php echo(time_ago($date)) ?></div>
</li>
<?php
}
echo "</ul>";

Related

multiple echo statements in one echo?

I am relatively new to PHP and want to know a more efficient way of echo'ing the following in less steps or even just one statement.
$sql = "SELECT * FROM comments";
$result = OpenCon()->query($sql);
while( $row = $result->fetch_assoc()) {
echo "<div class='card'><p class: card-text>";
echo $row['uid']."<br>";
echo $row['date']."<br>";
echo $row['message']."<br><br>";
echo "</p></div>";
As #droopsnoot has commented on above, you should try doing the next thing here.
while( $row = $result->fetch_assoc()) {
echo "<div class='card'><p class: card-text>". $row['uid']."<br>" . $row['date']."<br>".$row['message']."<br><br>". "</p></div>";
}
It should work perfectly.
Another option is to close php tag (?>) and use html markup with <?=$var?>:
<?php
$sql = "SELECT * FROM comments";
$result = OpenCon()->query($sql);
while( $row = $result->fetch_assoc()) {?>
<div class="card">
<p class="card-text">
<?=$row['uid']?><br>
<?=$row['date']?><br>
<?=$row['message']?><br><br>
</p>
</div>
<?php
} // end while

Grab and echo out multiple SQL rows with PHP

I'm wanting to fill a database with news article information such as PostID, PostTitle, PostDescription, PostContent and so on - I tried writing some code to constantly echo out the data in the SQL query but it only works for the first row that the query picks up
$SQL = "SELECT * FROM posts"; // get all info from posts
$result = mysql_query($SQL);
$row = mysql_fetch_array($result);
if (mysql_numrows($result) > 0) {
$row = mysql_fetch_array($result);
$Title = $row["PostTitle"];
$Description = $row["PostDescription"];
$Date = $row["PostDate"];
$AuthorID = $row["AuthorID"];
echo "<div id='preview'>";
echo "<div class='group selection'>";
echo "<div class='col span_1_of_3'>";
echo "<div id='previewmedia'>";
echo "</div>";
echo "</div>";
echo "<div class='col span_2_of_3'>";
echo "<h1>".$Title."</h1>";
echo "<p class='preview'>".$Description."</p>";
echo "<a href='bloglayout.php' class='orange readmore'>Read More...</a>";
echo "</div>";
echo "</div>";
echo "</div>";
}
I then thought about doing a SELECT query to then count the number of ID's in the table and then set that as $ID and then give each variable a number and then +1 after each loop but realised that wouldn't work so I'm kinda stuck on how to loop out several rows automatically from my database.
Also, I know I shouldn't be using mysql_numrows and whatnot in PHP and I should move to mysqli or PDO but this is for something where I've been asked to use mysql specifically.
You're only fetching one row. Try looping through your results.
$SQL = "SELECT * FROM posts"; // get all info from posts
$result = mysql_query($SQL);
while( $row = mysql_fetch_array($result);)
{
$Title = $row["PostTitle"];
$Description = $row["PostDescription"];
$Date = $row["PostDate"];
$AuthorID = $row["AuthorID"];
echo "<div id='preview'>";
echo "<div class='group selection'>";
echo "<div class='col span_1_of_3'>";
echo "<div id='previewmedia'>";
echo "</div>";
echo "</div>";
echo "<div class='col span_2_of_3'>";
echo "<h1>".$Title."</h1>";
echo "<p class='preview'>".$Description."</p>";
echo "<a href='bloglayout.php' class='orange readmore'>Read More...</a>";
echo "</div>";
echo "</div>";
echo "</div>";
}

Echo inside the html tag

The PHP only works on echoing the content when I echo it directly with the HTML tag (echo is outside the tag), as follows
include('db.php');
$blogurl="http://www.com/view/";
$results = mysql_query("SELECT * FROM product ORDER BY `id` DESC LIMIT 1");
while ($row = mysql_fetch_array($results)) {
echo "<h2>" . $row['title'] . "</h2>";
}
But it doesn't work when I try with this style:
<?php
include('db.php');
$results = mysql_query("SELECT * FROM product ORDER BY `id` ASC");
while ($row = mysql_fetch_array($results)) {
$blogurl="http://www.com/view";
$url=$row['url'];
$title=$row['title'];
?>
<td>
<?php echo $title;?>
</td>
<?php
}
?>
What I want is to change the way I echo the data from the database. But what is wrong with that second style?
I just solved it. I think the problem is because we can't get the content which has an extension of html from database. So the solution is I have to create a string or a var or (I dont know what we call it in php) by as follows:
<?php echo $blogurl;?>/<?php echo $title.".html"?>
The solution is that I don't need the row of url in my database. I just need to echo the title and give the ".html" behind it.
Thanks for anyone who has tried to helped me.
Cheers
Code should be:
<?php
include('db.php');
$results = mysql_query("SELECT * FROM product ORDER BY `id` ASC");
$blogurl="http://www.com/view";
while ($row = mysql_fetch_array($results)) {
$url=$row['url'];
?>
<td><?php echo $title; ?></td>
<?php
}
?>
Try this code
<?php
include('db.php');
$results = mysql_query("SELECT * FROM product ORDER BY `id` ASC");
$blogurl="http://www.com/view";
while ($row = mysql_fetch_array($results)) {
$url=$row['url'];
$title=$row['title'];
?>
<td><?= $title;?></td>
<?php
}
?>
Try this
<?php
include('db.php');
$blogurl="http://www.com/view";
$results = mysql_query("SELECT * FROM product ORDER BY `id` DESC LIMIT 1");
while ($row = mysql_fetch_array($results)) {
echo "<h2><a href='" . $blogurl."/".$row['url'] . "'>" . $row['title'] . "</a></h2>";
}
?>

PHP news system with mysql?

I am trying to make a news sytem in php.
The DB structure looks like this
"title" "author" "content"
And the PHP script looks like this
<?php
include ('config2.php');
$result = mysql_query("select * from news");
while($row = mysql_fetch_array($result)) {
$title = $row['title'];
$author = $row['author'];
$content = $row['content'];
}
echo"<div class=\"post\">
<h2 class=\"title\"><font color=\'#1e1e1e\' href=\"#\">$title</font></h2>
<p class=\"meta\">Today $author</p>
<p>$content</p>
</ul>
</div>"
?>
What I want to do is : Each time I insert a new row in my db, I want the new to be displayed on the page, and right now when I add a new row it just edit the previous new.
Any idea?
You're echoing outside the while loop. Modify it to the following:
while($row = mysql_fetch_array($result)) {
$title = $row['title'];
$author = $row['author'];
$content = $row['content'];
echo"<div class=\"post\">
<h2 class=\"title\"><font color=\'#1e1e1e\' href=\"#\">$title</font></h2>
<p class=\"meta\">Today $author</p>
<p>$content</p>
</ul>
</div>";
}
Put the echo statement inside the while loop.

Query fields won't separate when getting more than one at a time

When I run this query, it works fine but the individual fields would pull out. Like ThumbFilePath and Title.
If I run the query with only one field like:
$result = mysql_query("select ThumbFilePath from artwork where SCID = $SCID") or die(mysql_error());
It works fine. Any ideas why I can't pull the other fields?
<?php
$dbname = 'pdartist2';
$table = 'artowrk';
// query
$result = mysql_query("select AID, ThumbFilePath, Title, DisplayOrder from artwork where SCID = $SCID") or die(mysql_error());
while($row = mysql_fetch_row($result))
{
foreach($row as $cell)
{
echo "<div id='thumb_container'>";
echo "
<a href='gallery_detail.php?AID=$AID'>
<img src='http://markdinwiddie.com/PHP2012/$ThumbFilePath' title='Enlarge' alt='Enlarge' border='0'>
</a>
";
echo "$Title";
echo "</div>";
}
}
mysql_free_result($result);
?>
In general, you'll ALWAYS need to dereference the columns you need. For example:
while($row = mysql_fetch_row($result)) {
$aid = $row['AID'];
$tpath= $row['ThumbFilePath'];
$title = $row['title'];
...
<?php
$dbname = 'pdartist2';
$table = 'artowrk';
// query
$sql = "select AID, ThumbFilePath, Title, DisplayOrder from artwork where SCID = $SCID";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_row($result)) {
$AID = $row['AID'];
$ThumbFilePath= $row['ThumbFilePath'];
$Title = $row['Title'];
$DisplayOrder = $row['DisplayOrder'];
echo "<div id='clear'></div>";
echo "<div id='thumb_container'>";
echo "<a href='gallery_detail.php?AID=$AID'><img src='http://markdinwiddie.com/PHP2012/$ThumbFilePath' title='Enlarge' alt='Enlarge' border='0'></a>";
echo "<div id='name_spacer'></div>";
echo "<div id='thumbdesc'>";
echo "$Title";
echo "</div>";
echo "</div>";
}
mysql_free_result($result);
?>

Categories