$sql = "SELECT * from post where forum_id = $_GET[id]";
$result = mysqli_query($conn, $sql) or die('Error querying database.');
while ($row = mysqli_fetch_array($result)) {
echo '' . $row['fourm_id'];
echo '<div id="post3">
<p class="1">
<span class="name">'.'
' .$row['name']. '</span>'.
'<span class="trip">
' .' !'
. $row['title'].''.
' </span>'.
' <span class="time">
' .$row['time']. '' .
' </span>'.
' </p>
<p class="2">
<span class="texts">
' .$row['texts']. '' .
' </span>'.
' </p>'.
if (!isset($_SESSION["user_id"])) {
}
else {
'<a href="delete_post.php?fourm_id=' . $row['id']. '>Delete</a>' . }
' </div>';
}
mysqli_close($conn);
?>
The mistake in the code is obvious, just showing what I am trying to do.
This echo I have here displays the users post, how would I go about sticking an isset in there so only admins can see the delete link? Or is there another way of doing it outside the echo with out it going outside the user post?
<?
$data = array();
$sql = "SELECT * from post where forum_id = ".intval($_GET['id']);
$res = mysqli_query($conn, $sql) or trigger_error(mysqli_error($conn));
while ($row = mysqli_fetch_array($result)) {
$data[] = $row;
}
?>
<?php foreach($data as $row): ?>
<?=$row['fourm_id']?>
<div id="post3">
<p class="1">
<span class="name"><?=$row['name']?></span>
<span class="trip"> !<?=$row['title']?></span>
<span class="time"><?=$row['time']?></span>
</p>
<p class="2">
<span class="texts"><?=$row['texts']?></span>
</p>
<? if (isset($_SESSION["user_id"])): ?>
Delete
<? endif ?>
</div>
<? endforeach ?>
If you want to do it inline, you can either use the ternary operator or something like sprintf.
Ternary operator (best if you only have a couple of insertions):
echo "Foo ".(isset($bar) ? "bar" : "");
(s)printf (best if you have several insertions):
printf("Foo %s", isset($bar) ? "bar" : "");
However this does not scale very much, so when you have large-scale "string construction" it's much saner to split the output into more than one statements:
echo "Foo ";
if (isset($bar)) {
echo "bar";
}
This is best if you have lots of insertions.
Related
I want to fetch results of a MySQL database and put them into "pages" in my webpages.
What I have is a bootstrap webpage and I'm using items to show the posts but I want to show let's say 5 entries per item and instead of making pages I want to sort them into items to show the entries.
Is this possible and how is it possible?
What I have now and does work is the following code:
<div class="item">
div class="testimony-slide active text-center">
<?php
$sql = "SELECT name,email,message FROM guestbook ORDER BY ID DESC LIMIT 5 OFFSET 0";
$result = mysqli_query($conn, $sql) or die ("Could not access DB: " . mysql_error());
while ($row = mysqli_fetch_assoc($result)) {
echo "<figure>";
echo "<img src=\"../images/guestbook.png\">";
echo "</figure>";
echo !empty($row['name']) ? ("<span>". $row['name'] . "</span>") : '';
echo !empty($row['email']) ? ("<span>". $row['email'] . "</span>") : '';
echo "<blockquote>";
echo !empty($row['message']) ? ("<p>". $row['message'] . "</p>") : '';
echo "</blockquote>";
}
mysqli_close($con);
?>
</div>
</div>
<div class="item">
div class="testimony-slide active text-center">
<?php
$sql = "SELECT name,email,message FROM guestbook ORDER BY ID DESC LIMIT 5 OFFSET 5";
$result = mysqli_query($conn, $sql) or die ("Could not access DB: " . mysql_error());
while ($row = mysqli_fetch_assoc($result)) {
echo "<figure>";
echo "<img src=\"../images/guestbook.png\">";
echo "</figure>";
echo !empty($row['name']) ? ("<span>". $row['name'] . "</span>") : '';
echo !empty($row['email']) ? ("<span>". $row['email'] . "</span>") : '';
echo "<blockquote>";
echo !empty($row['message']) ? ("<p>". $row['message'] . "</p>") : '';
echo "</blockquote>";
}
mysqli_close($con);
?>
</div>
</div>
The problem with this code is that it makes me do it manually, so that makes it a pain in the ass... I want it to be split into items according to the entries found in the database.
I tried solutions i found on google, but they all are made for seperate pages and not making the results split inside divs.
Use a counter variable to keep track of how many rows are printed, and segregate the rows into different divs accordingly. So your code should be like this:
<div class="item">
<div class="testimony-slide active text-center">
<?php
$sql = "SELECT name,email,message FROM guestbook ORDER BY ID DESC";
$result = mysqli_query($conn, $sql) or die ("Could not access DB: " . mysqli_error($conn));
$counter = 1;
while ($row = mysqli_fetch_assoc($result)) {
echo "<figure>";
echo "<img src=\"../images/guestbook.png\">";
echo "</figure>";
echo !empty($row['name']) ? ("<span>". $row['name'] . "</span>") : '';
echo !empty($row['email']) ? ("<span>". $row['email'] . "</span>") : '';
echo "<blockquote>";
echo !empty($row['message']) ? ("<p>". $row['message'] . "</p>") : '';
echo "</blockquote>";
if($counter % 5 == 0){
echo '</div></div>';
echo '<div class="item"><div class="testimony-slide active text-center">';
}
++$counter;
}
mysqli_close($con);
?>
</div>
</div>
Sidenote: Don't mix mysql and mysqli APIs, those are two different things. mysql_error() should be mysqli_error($conn).
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
I have the following code, but keep running into a syntax error on line 7, saying "Parse error: syntax error, unexpected 'htmlentities' (T_STRING)." Is this not the correct way to echo php and html at the same time? Any help is appreciated!
<?php
$stmt = $pdo->query("SELECT first_name, last_name, department, website filename FROM Profile");
while ( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) {
echo('<div class="col-sm-3">
<img class="img-rounded" src="'.$row['filename'].'"/>
<p class="caption">');
echo(''.($row['first_name']).' '.htmlentities($row['last_name']).'')
echo(', '.htmlentities($row['department']));
echo('</p> </div>');
}
?>
Your echo should be without parenthesis and you're missing . on a few of your echo commmands.
To join an echo string (concatenate) you need to add the strings with a "."
Please use :
<?php
$stmt = $pdo->query("SELECT first_name, last_name, department, website filename FROM Profile");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo '<div class="col-sm-3">
<img class="img-rounded" src="' . $row['filename'] . '"/>
<p class="caption">';
echo '' . ($row['first_name']) . ' ' . htmlentities($row['last_name']) . '';
echo ', ' . htmlentities($row['department']);
echo '</p> </div>';
}
?>
You should concatenate your strings when you interrupt the string like
echo('<a href="' . htmlentities($row['website']) . '">'.($row['first_name']).' '
At the htmlentities part you are missing the dots for stringconcatenation
You have concatenated the string in wrong way. Try following
<?php
$stmt = $pdo->query("SELECT first_name, last_name, department, website, filename
FROM Profile");
while ( $row = $stmt->fetch(PDO::FETCH_ASSOC) )
{
echo '<div class="col-sm-3">
<img class="img-rounded" src="'.$row['filename'].'"/>
<p class="caption">';
echo ''.$row['first_name'].' '.htmlentities($row['last_name']).'';
echo ', '.htmlentities($row['department']);
echo '</p> </div>';
}
?>
OR
You can also try the following as an alternative way..
<?php
$stmt = $pdo->query("SELECT first_name, last_name, department, website, filename
FROM Profile");
while ( $row = $stmt->fetch(PDO::FETCH_ASSOC) )
{
?>
<div class="col-sm-3">
<img class="img-rounded" src="<?php echo $row['filename'] ?>"/>
<p class="caption">
<a href="<?php echo htmlentities($row['website']) ?>">
<?php echo $row['first_name'].' '.$row['last_name'] ?>
</a>
<?php echo ', '.htmlentities($row['department']); ?>
</p>
</div>
<?php
}
?>
I edit the code you try it. You miss . and ;
echo '<div class="col-sm-3">
<img class="img-rounded" src="'.$row['filename'].'"/>
<p class="caption">';
echo ''.($row['first_name']).' '.htmlentities($row['last_name']).'';
echo ', '.htmlentities($row['department']);
echo '</p> </div>';
I have user information coming from database on a profile page using a while loop. I want to be able to write stuff like this, $username's profile. I can't seem to figure it out. Here is my code.
<?php
//open database connection
include 'page-start.php';
include 'core/init.php';
?>
<?php
$myQuery = ("SELECT user_id, username, profile, city FROM `users` WHERE user_id = '" . mysql_real_escape_string($_GET['ID']) . "' ") or die(mysql_error());
//run query
$result = $con->query($myQuery);
if (!$result) die('Query error: ' . mysqli_error($result));
if($result === FALSE) {
die(mysql_error()); // TODO: better error handling
} ?>
<?php require_once $_SERVER['DOCUMENT_ROOT'] . '/studentsupport/defines.php'; ?>
<?php include_once("head.php");
while ($row = $result->fetch_array(MYSQLI_ASSOC))
{
echo '<div class="sixteen columns" id="user-profile">';
echo'<h2 class="username"> ' . $row['username'] . ' </h2> ';//bob's Profile
echo'<p>' . $row['city'] . '</p>'; //city: london
echo '<div class="eight columns" id="user-profile-img">';
echo'<img src="'. $row['profile'] . '"/>';
echo '</div>';
echo '</div>';
}
?>
edit: sorry I didn't explain it very well.
I want to be able to have some information come from the database and some information just as standard html for example:
<p><?php echo $username; ?>'s profile </p>
<p>city: <?php echo $city; ?> </p>
During each loop in the while, $row['username'] will have a username.
If you want to be able to get all the usernames later, add the following:
$users[] = $row['username'];
Now, Everywhere in your script $users[0] will have the 1st username, $users[1] will have the second, etc.
If you are beginner than i wil say You should use easy method now it seems okay
like You make Whole page of profile like you want
like
then you can Take your data in Variables in while Loop like that
while ($row = $result->fetch_array(MYSQLI_ASSOC))
{
$profile = $row['profile'];
$name = $row['name'];//you can take as many variables as you want
$city = $row['city'];
}
then you can use these variables in html simply
<div class="sixteen columns" id="user-profile">
<h2 class="username"><?php echo $name; ?> </h2>
</div>
start like that next step like above will get easy to u :)
This may do it.
<?php include_once("head.php");
while ($row = $result->fetch_array(MYSQLI_ASSOC))
{ ?>
<div class="sixteen columns" id="user-profile">
<h2 class="username"><?php echo $row['username']; ?></h2>
<p><?php echo $row['city']; ?></p>
<div class="eight columns" id="user-profile-img">
<img src="<?php echo $row['profile']; ?>"/>
</div>
</div>
<? } ?>
This code
<?php echo "Likes: ".$r['votes_up']." "; echo "Dislike: ".$r['votes_down'].""; ?>
Wont post the values from the table for 'votes_up' 'votes_down'
I cant get my head round this! Ive got this exact code working on a different page but it wont on this.
Heres the entire code ....
<div class="message">
<?php
$sql = mysql_query("SELECT * FROM threads WHERE id = '" . $_GET['id'] . "'") or die(mysql_error());
while($r = mysql_fetch_array($sql)) {
$posted = date("jS M Y h:i",$r['posted']); echo "".$r['author']." $posted";?>
<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-text="<?php echo "".$r['message'].""; ?>">
Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
<div class="message2"><?php echo "".$r['message'].""; }?></div>
<?php echo "Likes: ".$r['votes_up']." "; echo "Dislike: ".$r['votes_down'].""; ?>
</div>
<br/>
<hr>
Can anyone help? its driving me insane
Line 8 of your code
<div class="message2"><?php echo "".$r['message'].""; }?></div>
Why is that closing curly brace in there before the closing ?> ?
You seem to have closed the WHILE loop here:
<div class="message2"><?php echo "".$r['message'].""; }?></div>
Notice the curly bracket.
Therefore, votes_up and votes_down have no values.
Your curly bracket is being placed before the last echo statement, therefore the $r variable is out of scope.
Move the } to later in your page like so
<div class="message">
<?php
$sql = mysql_query("SELECT * FROM threads WHERE id = '" . mysql_real_escape_string($_GET['id']) . "'") or die(mysql_error());
while($r = mysql_fetch_array($sql))
{
$posted = date("jS M Y h:i",$r['posted']);
echo $r['author']." ".$posted;
?>
<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-text="<?php echo $r['message']; ?>">
Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
<div class="message2">
<?php
echo $r['message'];
?>
</div>
<?php
echo "Likes: ".$r['votes_up']." ";
echo "Dislike: ".$r['votes_down'];
}
?>
</div>
<br/>
<hr>
Also notice the call to mysql_real_Escape_string in the $sql var. this will prevent nasty sql injections
This is because the $r['...'] variable is out of the scope where you place it.
Better way to do it (assuming you only fetch one row:
<?
$sql = mysql_query("SELECT * FROM threads WHERE id = '" . $_GET['id'] . "'") or die (mysql_error());
$row = mysql_fetch_assoc($sql);
?>
<div> ... </div>
<?php echo "Likes: " . $row['votes_up'] . " Dislike: " . $row['votes_down']; ?>
I'm looking for some kind of Nav tab (vertical) that allows it to be populated by SQL. For example, in the SQL table there would be a column for title (which is the title of the tab) and a column for the contents of the tab.
I have tried building one myself, but I have no idea how to fit it in a nav bar because usually I'd do a while loop to populate something with SQL, but because nav's have two things that need populating (the <li> and the <div> bits), I am unsure of how to do it.
Thanks.
Edit:
<ul>
<?php
$query = tep_db_query("select * table1");
while ($row = mysql_fetch_assoc($query)) {
echo '<li>' . $row['tabtitle'] . '</li>'
}
?>
</ul>
<?php
$query2 = tep_db_query("select * table1");
while ($row = mysql_fetch_assoc($query2)) {
echo '
<div id="tabs-' . $row['tabid'] . '">
<p> ' . $row['tabcontent'] . ' </p>
'
}
?>
What you have seems like it would be okay, you might be able to do something like this in order to eliminate half of your db calls.
//Get information from db.
<?php
$query = tep_db_query("SELECT tabid, tabtitle, tabcontent FROM table1");
while ($row = mysql_fetch_assoc($query)) {
$table[] = $row;
}
?>
<ul>
<?php foreach($table as $row){
echo '<li>' . $row['tabtitle'] . '</li>';
}
?>
</ul>
<?php foreach($table as $row){
echo '
<div id="tabs-' . $row['tabid'] . '">
<p> ' . $row['tabcontent'] . ' </p>
';
}
?>