Reputation: 886
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><a href=" . $blogurl . $row['url'] . ">" . $row['title'] . "</a></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>
<a href="<?php echo $blogurl;?>/<?php echo $url;?>"><?php echo $title;?></a>
</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?
Upvotes: 1
Views: 203
Reputation: 886
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
Upvotes: 1
Reputation: 722
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>";
}
?>
Upvotes: 0
Reputation: 7
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><a href="<?php echo $blogurl.'/'. $url;?>"><?= $title;?></a></td>
<?php
}
?>
Upvotes: 0
Reputation: 3034
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><a href="<?php echo $blogurl;?>/<?php echo $url;?>"><?php echo $title; ?></a></td>
<?php
}
?>
Upvotes: 0