Populating Navigation Tabs with SQL? - php

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

Related

How to put MySQL results in Bootstrap items

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).

I am trying to print all the database users in a Unordered list in PHP. But it is not working

<ul>
<?php
$sql = "SELECT first_name, last_name FROM loginsystem";
if ($result = mysqli_query($connection, $sql)) {
if ($total_rows = mysqli_num_rows($result)) {
while ( $row = mysqli_fetch_assoc($result) ) : ?>
<li>
<?php $row['first_name'] . " " . $row['last_name']; ?>
</li>
<?php
endwhile;
mysqli_free_result($result);
}
}
?>
</ul>
The above code is not working. Please help to solve this issue. Please help me
You are not actually outputing anything in your while loop, you need only add an echo to this line
<?php echo $row['first_name'] . " " . $row['last_name']; ?>

Looping 2 set of results twice

I have a page where I need to pull the titles of 3 of the latest records in one bootstrap row and then few lines further in the next row I need to pull the image of each record (only 3) again.
How can I do this?
On your loop construct two series of HTML entries, one for your titles and another one for your pictrures. Then echo each HTML string to the div you want them to be.
<?php
titlesHTML = "";
imagesHTML = "";
foreach ($result as $row) {
$contentTitle = $row['cont_title'];
$contentImage = $row['cont_picture'];
$titlesHTML .= "<p>" . $contentTitle . "</p>";
$imagesHTML .= "<p>" . $contentImage . "</p>";
// if you want not to echo the image location but the image itself try the following code instead:
// $imagesHTML .= "<img src='" . $contentImage . "'>";
}
?>
<div class="row">
<div class="col-em-12">
<?php echo $titlesHTML; ?>
</div>
<div>
<div class="row">
<div class="col-em-12">
<?php echo $imagesHTML; ?>
</div>
<div>

how to get data to html <ul> <li> </li> </ul> list from mysql database with php

I have a video player and want to populate video on it from mysql database, here is my code.
<ul id="playlist1" style="display:none;">
<li data-thumb-source="assets/img/ddd.jpg" data-video-source="assets/video/ddd.mp4" data-poster-source="assets/img/ddd.jpg" data-downloadable="yes">
<div data-video-short-description="">
<div>
<p class="minimalDarkThumbnailTitle">ddd</p>
<p class="minimalDarkThumbnailDesc">dddd.</p>
</div>
</div>
<div data-video-long-description="">
<div>
<p class="minimalDarkVideoTitleDesc">dddd</p>
<p class="minimalDarkVideoMainDesc">ddd</p>
<p>For more information about this please follow this link</p>
</div>
</div>
</li>
</ul>
My MySQL database has the following columns
id
data-thumb-source
data-video-source
data-poster-source
minimalDarkThumbnailTitle
minimalDarkThumbnailDesc
My PHP code to get the data from the database:
<?php
$result= mysql_query("select * from video order by id DESC" ) or die (mysql_error());
while ($row= mysql_fetch_array ($result) ){
$id=$row['id'];
?>
However, my video player is blank because it has nothing to play.
Edit: I used this code to display my value
<ul>
<?php
$sql = "SELECT * FROM `mytable`";
foreach ($db->query($sql) as $row) {
$li = '<li> data-thumb-source="' .$row['data-thumb-source']. '"';
$li .= ' class="playlistItem" data-type="local"';
$li .= ' data-video-source="' .$row['data-video-source']. '"';
$li .= ' minimalDarkThumbnailTitle="' .$row['minimalDarkThumbnailTitle']. '"';
$li .= ' minimalDarkThumbnailDesc="' .$row['minimalDarkThumbnailDesc']. '"';
echo $li;
}
$db = null;
?>
</ul>
This did not make a difference though.
You closed your opening li tag before defining the attributes.
<ul>
<?php
$sql = "SELECT * FROM `mytable`";
foreach ($db->query($sql) as $row) {
$li = '<li data-thumb-source="' .$row['data-thumb-source']. '"';
$li .= ' class="playlistItem" data-type="local"';
$li .= ' data-video-source="' .$row['data-video-source']. '"';
$li .= ' minimalDarkThumbnailTitle="' .$row['minimalDarkThumbnailTitle']. '"';
$li .= ' minimalDarkThumbnailDesc="' .$row['minimalDarkThumbnailDesc']. '"';
echo $li . '>descriptions</li>';
}
$db = null;
?>
</ul>

isset within an echo? Or is there a better way?

$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.

Categories