Echo inside the html tag - php

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>";
}
?>

Related

View data to another page in PHP

I have a problem for display data from page to another page.
This is index.php:
<?php
include '../php/connect.php';
$query = mysql_query("SELECT * FROM user
ORDER BY user.id_user DESC") or die(mysql_error());
if(mysql_num_rows($query) == 0){
echo '<tr><td colspan="6">Tidak ada data!</td></tr>';
}else{
while($data = mysql_fetch_assoc($query)){
echo '<tr>';
echo '<td>'.$data['id_user'].'</td>';
echo '<td>'.$data['name'].'</td>';
echo '<td>'.$data['email'].'</td>';
echo '<td>View File</td>';
echo '<td>Konfirmasi</td>';
echo '</tr>';
?>
<?php
}}
?>
This is confirm_pembayaran.php
<?php
include '../php/connect.php';
$query = mysql_query("SELECT * FROM user
WHERE id_user=$id_user") or die(mysql_error());
if(mysql_fetch_array($query) == 0){
echo '<tr><td colspan="6">Tidak ada data!</td></tr>';
}else{
while($data = mysql_fetch_assoc($query)){
echo '<tr>';
echo '<td>'.$data['id_user'].'</td>';
echo '<td>'.$data['name'].'</td>';
echo '<td>'.$data['email'].'</td>';
echo '</tr>';
?>
<?php
}}
?>
The problem is in confirm_pembayaran.php for id_user that i was click from index.php not display in confirm_pembayaran.php. What should i do for confirm_pembayaran.php?
On the index page change the link to something like this ( unless you have set your .htaccess file up to accept links as they were generated )
You should, however, not be using the mysql_ family of functions as they have been deprecated. The code, as it is now is vulnerable to sql injection - I merely posted this to show how to pass the user_id parameter from one page to another which was what ( I think ) you wanted
/* index.php */
Konfirmasi
/* confirm_pembayaran.php */
$user_id=isset( $_GET['user_id'] ) ? filter_input( INPUT_GET,'user_id', FILTER_SANITIZE_STRING ) : false;
$query = mysql_query("SELECT * FROM user
WHERE id_user='{$user_id}'") or die(mysql_error());
First you need to pass your user id in query string correctly like below:
index.php
Changes this line
echo '<td>Konfirmasi</td>';
To
echo '<td>Konfirmasi</td>';
Then you need to change confirm_pembayaran.php
<?php
include '../php/connect.php';
$id_user = $_GET['id_user']; // Add this line for get your user id
$query = mysql_query("SELECT * FROM user
WHERE id_user=$id_user") or die(mysql_error());
if(mysql_fetch_array($query) == 0){
echo '<tr><td colspan="6">Tidak ada data!</td></tr>';
}else{
while($data = mysql_fetch_assoc($query)){
echo '<tr>';
echo '<td>'.$data['id_user'].'</td>';
echo '<td>'.$data['name'].'</td>';
echo '<td>'.$data['email'].'</td>';
echo '</tr>';
?>
<?php
}}
?>
Simplest way is:
In index.php
Konfirmasi
in confirm_pembayaran.php
$id_user = intval($_GET['userId']);

Generate title from database and show related post

I have a DB name askadoc , where people can ask about their problem. I want to show all question(or you can say the titles) together in a page. And then when user click on a question, the question will show in a different page with it's comments/details. To do this i have tried the below code and its working perfectly. but how can i do it without using button ?
<?php
$comment = "SELECT * FROM `askadoc` ";
$result = mysqli_query($conn, $comment);
$question = "";
$id="";
if(mysqli_num_rows($result)>0){
while($row = mysqli_fetch_assoc($result)) {
$id = $row["id"];
$question = $row["question"];
?>
<form action="post.php" method="post">
<?php
echo $id;
echo "<input type='hidden' name = 'id' value = ".$id.">";
echo "<button>".$question."</button>";
echo "<br>";
?>
</form>
<?php
}
}
?>
I think you don't need form this. You can user anchor like this.
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$id = $row["id"];
$question = $row["question"];
?>
<?php echo $question ?>
<?php
}
}
Now when you click this anchor, you get question id inside post.php, so easily you can display a particular question comments.
You can use anchor tag then pass the question id to post.php
if(mysqli_num_rows($result)>0){
while($row = mysqli_fetch_assoc($result)) {
$id = $row["id"];
$question = $row["question"];
?>
<?php
echo "<a href='post.php?question_id=".$id."' target='_blank'>".$question."</a>";
echo "<br>";
?>
<?php
}
}
Then to get the value of question_id use $_GET['question_id'];
$questionId = $_GET['question_id']
and its better to check if question_id does exist.
$questionID = isset($_GET['question_id'])? $_GET['question_id'] : '';
if(!empty($questionID)){
//Use $questionID here to query some data from database
}

PHP MySQL display data by id from database - freedom placement

I would like to have the freedom to place a row entry from my database wherever i' prefer in the page. Right now, the php code that I use is as follows (it is clean working code):
<html><head></head>
<body>
<?php
$db = mysql_connect("xxx","xxx","xxx") or die("Database Error");
mysql_select_db("caisafety",$db);
$id = $_GET['id'];
$id = mysql_real_escape_string($id);
$query = "SELECT * FROM `cert_rr` WHERE `id`='" . $id . "'";
$result = mysql_query($query);
echo $row['id']; while($row = mysql_fetch_array( $result )) {
echo "<br><br>";
echo $row['basic3'];
echo $row['basic2'];
echo $row['basic1'];
}
?>
</body>
</html>
I call id through the browser Eg. http://site.com/getid.php?id=10 . But I do not have the freedom to place my row entry within my html. For eg. like this:
<table><tr>
<td align="center">BASIC INFO 1: <?php echo $row['basic1']; ?></td>
<td align="center">BASIC INFO 2: <?php echo $row['basic2']; ?></td>
</tr></table>
I can place html within echo PHP tags but then I have to clean up my html and thats a lot of work. Retaining HTML formatting would be preferred. Any help or guidelines on this would be much appreciated.
<?php
$db = mysql_connect("xxx","xxx","xxx") or die("Database Error");
mysql_select_db("caisafety",$db);
$id = $_GET['id'];
$id = mysql_real_escape_string($id);
$query = "SELECT * FROM `cert_rr` WHERE `id`='" . $id . "'";
$result = mysql_query($query);
//you need to retrieve every row and save to an array for later access
for($rows = array(); $tmp = mysql_fetch_array($result);)
{
$rows[] = $tmp;
}
//now you can use the $rows array where every you want e.g. with the code from Zhube
?>
....
<table><?php foreach($rows as $r):
<td><?php echo $r['id'] ?></td><?php endforeach ?>
</table>
By
while($row = mysql_fetch_array( $result )) {
echo "<br><br>";
echo $row['basic3'];
echo $row['basic2'];
echo $row['basic1'];
}
you save only the last row
Instead of having your HTML tags as part of the PHP variable, do something like this instead:
<table><?php foreach($row as $r):?>
<td><?php echo $r['id'] ?></td><?php endforeach ?>
</table>

Using echo with lists in 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>";

Display only one entry from the database with PHP

The following code retrieves and displays the correct data from the database however it gets all the data. I need a way to assign each value it retrieves from the database to a PHP variable. For example, if it gets "Joe", "Henry", and "Robert" from the database, I'd like one variable for each of those and right now it returns and array with all the values.
<?php
dbCon();
$query1 = mysql_query("SELECT * FROM hosts WHERE name!=''");
while($row1 = mysql_fetch_assoc($query1)) {
$res = $row1['name'] . '<br />';
echo $res;
}
?>
<?php
function echoName($id) {
dbCon();
$query1 = mysql_query("SELECT * FROM hosts WHERE id='$id'");
while($row1 = mysql_fetch_assoc($query1)) {
$res = $row1['name'] . '<br />';
echo $res;
}
}
?>
<div id="joe_div">
<?
echoName("1");
?>
</div>
.....
<div id="henry_div">
<?
echoName("2");
?>
</div>
Declare variable variables as such:
while($row1 = mysql_fetch_assoc($query1)) {
$$row1['name'] = $row1['name']
}
echo $Joe; //returns Joe
Though I don't know why you would ever need that
if it gets "Joe", "Henry", and "Robert" from the database, I'd like one variable for each of those
So try this:
<?php
dbCon();
$query1 = mysql_query("SELECT * FROM hosts WHERE name!=''");
while($row1 = mysql_fetch_assoc($query1)) {
$$row1['name'] = $row1['name'];
}
echo $Joe;
echo $Henry;
echo $Robert;
?>
P.S: I don't know why you want to do this, but i am sure you have a better approach to solve your problem.

Categories