I got a problem with showing things when pressing on a topic.
What I want
I want too show the records out of the second database that has the correct topic_id so if you click on a topic it needs to only show the "onderwerpen"(dutch for subjects) that have that topic_id
My code:
<div class="box box-info">
<div class="panel panel-default">
<div class="panel-heading main-color-bg">
<h3 class="panel-title">Topics</h3>
</div>
<div class="panel-body">
<?php
$toppics = $app->get_topics();
foreach($toppics as $topic){
echo '<div class="col-md-6">';
echo '<div class="well dash-box">';
echo '<h3>'.$topic['onderwerp'].'</h3><br>';
echo '' .$topic['omschrijving'].'';
echo '</div>';
echo '</div>';
}
?>
</div>
</div>
</div><!-- /.box -->
Function I use:
public function get_topics(){
$getTopic = $this->database->query("SELECT * FROM topics ORDER BY id DESC");
$topics = $this->database->resultset();
return $topics;
}
The topic database
The onderwerpen database
After clicking on a subject It now shows all the "onderwerpen" out of the database
The code for that:
<?php
$onderrwerp = $app->get_onderwerpen();
foreach($onderrwerp as $onderwerp){
echo '<div class="well well-sm">';
if(file_exists('assets/images/profielfotos/'.$onderwerp['klant_id'])) {
echo '<img class="img-circle" src="/assets/images/profielfotos/'.$onderwerp['klant_id'].'/'.$onderwerp['foto'].'" />';
} else {
echo '<i style="font-size: 2.5em;" class="fas fa-user-circle"></i>';
}
echo '<b> '.$onderwerp['onderwerpnaam'].'</b>';
echo ' - ' . $onderwerp['voornaam'] . ' ' . $onderwerp['achternaam'];
echo '</div>';
}
?>
Get_onderwerpen function:
public function get_onderwerpen(){
$getOnderwerp = $this->database->query("
SELECT onderwerpen.*, klanten.foto, klanten.voornaam, klanten.achternaam FROM onderwerpen
LEFT JOIN klanten ON onderwerpen.klant_id=klanten.id
ORDER BY id ASC");
$onderwerpen = $this->database->resultset();
return $onderwerpen;
}
Related
Currently creating 2 dropdown menus, one for category and one for subcategory. My current function shows all data on the page only for subcategories but not for categories. Why is this happening?
Current Functionality: User selects a category, page refreshes and is blank. Once user selects SUBcategory it shows all products in that subcategory.
Desired Functionality: User selects a category, page refreshes and shows all products in that category. Once user selects subcategory it shows all products in that subcategory.
function populate_search_catsub()
{
global $link;
$subcatt = "";
$query = 'SELECT * FROM item';
$result = mysqli_query($link, $query);
$catt = $_GET['catt'];
if (isset($_GET['subcatt'])) {
$subcatt=$_GET['subcatt'];
}
$nbprod = mysqli_query($link, "SELECT * FROM `item` WHERE cat='$catt' AND field='$subcatt'");
if (!isset($_GET['catt']) || $_GET['catt'] == '') {
unset($_GET['catt'], $_GET['submitsearchsub']);
populate_main();
} else {
if (isset($_GET['subcatt'])) {
echo '<span>Search results for Category="'.$catt.' And Sub Category='.$subcatt.'"</span><hr>';
}
$result = mysqli_query($link,"SELECT * FROM `item` WHERE cat='$catt' AND field='$subcatt'");
if ($cat = mysqli_fetch_row($result)) {
echo '
<div class="itemlist">
<span><h3>'.$cat[1].'</h3><h6><u>View</u></h6></span>
<div class="col-lg-12" style="background-color: white;"><br>
<div class="row">
<div class="col-lg-12" style="margin-right: 2%;">
<img src="https://via.placeholder.com/160x210">
</div>
</div><br>
</div>
</div>
<hr>
';
while ($cat = mysqli_fetch_row($result))
echo '
<div class="itemlist">
<span><h3 style="display:inline;">'.$cat[1].'</h3><h6 style="display:inline; margin-left: 1%;"><u>View</u></h6></span>
<div class="col-lg-12" style="background-color: white;"><br>
<div class="row">
<div class="col-lg-2" style="margin-right: 2%;">
<img src="https://via.placeholder.com/160x210">
</div>
</div><br>
</div>
</div>
<hr>
';
} else {
if (isset($_GET['subcatt'])) {
echo "<h2 >No results found</h2>";
}
unset($_GET['catt'], $_GET['submitsearchsub']);
populate_main();
}
}
}
First of all, grab $_GET['catt'] and $_GET['subcatt']. Then, you can build a query based on these parameters. And please, use mysqli_num_rows to check if there are more than zero rows returned.
function populate_search_catsub()
{
global $link;
$catt = $_GET['catt'] ?? ''; // Requires PHP 7.0 or upper.
$subcatt = $_GET['subcatt'] ?? ''; // Requires PHP 7.0 or upper.
$query = 'SELECT * FROM `item`'; // Initial query
if (!$catt) {
unset($_GET['submitsearchsub']);
populate_main();
} else {
$query .= " WHERE `cat` = '{$catt}'";
if ($subcatt) {
echo '<span>Search results for Category="' . $catt . ' And Sub Category=' . $subcatt . '"</span><hr>';
$query .= " AND `field` = '{$subcatt}'";
}
$result = mysqli_query($link, $query);
if (mysqli_num_rows($result) > 0) { // Check if result set is not empty.
while ($cat = mysqli_fetch_row($result))
echo '
<div class="itemlist">
<span><h3 style="display:inline;">'.$cat[1].'</h3><h6 style="display:inline; margin-left: 1%;"><u>View</u></h6></span>
<div class="col-lg-12" style="background-color: white;"><br>
<div class="row">
<div class="col-lg-2" style="margin-right: 2%;">
<img src="https://via.placeholder.com/160x210">
</div>
</div><br>
</div>
</div>
<hr>
';
} else {
if ($subcatt) {
echo '<h2 >No results found</h2>';
}
unset($_GET['catt'], $_GET['submitsearchsub']);
populate_main();
}
}
}
I was wondering how to get a limit on the displayed records and for example a limit of 10 and after that their needs to be a button to get all the topics their are. I was wondering how to display it that way
Functions code:
public function get_topics(){
$getTopic = $this->database->query("
SELECT topics.*, klanten.foto, klanten.voornaam, boards.topic, ledenpagina.ledenpagina_id, klanten.achternaam FROM topics
LEFT JOIN ledenpagina ON ledenpagina.ledenpagina_id = topics.ledenpagina_id
LEFT JOIN klanten ON topics.klant_id=klanten.id
LEFT JOIN boards ON topics.board_id=boards.id
WHERE ledenpagina.ledenpagina_id=:ledenpagina_id
ORDER BY id ASC");
$this->database->bind(":ledenpagina_id", $_SESSION['ledenpagina_id']);
$topics = $this->database->resultset();
return $topics;
}
The php code:
<section class="col-md-8 connectedSortable">
<div class="clearfix paddingbottom10"></div>
<div class="box box-info">
<div class="panel panel-default">
<div class="panel-heading main-color-bg">
<h3 class="panel-title">Topics</h3>
</div>
div class="panel-body">
<?php
$toppic = $app->get_topics();
foreach($toppic as $topic){
echo '<a href="https://####/reactie"> <div id="topic">';
echo '<div id="topicimg">';
if(file_exists('assets/images/profielfotos/'.$topic['klant_id'])) {
echo '<img class="img-circle" src="/assets/images/profielfotos/'.$topic['klant_id'].'/'.$topic['foto'].'" />';
} else {
echo '<i class="fa fa-fw fa-user img-circle"></i>';
}
echo '</div><div id="topictekst">';
echo '<b>'.$topic['topicnaam'].'</b>';
echo ' - ' . $topic['voornaam'] . " " . $topic['achternaam'] ;
echo '<span style="float:right; margin-top:15px; margin-left:5px;">'.implode($app->count_reactie($topic['id'])) .' reacties</span> <span style="float:right; color:lightgrey; margin-top:15px"class="fa fa-comment"></span>';
echo '<hr><span class="badge bg-red">' . implode($app->boards($topic['board_id'])) . '</span>';
echo ' laatste reactie: ' .$app->tijd_reactie($topic['id']) . ' door ' .$app->reactieDoor($topic['id']);
echo '</div></div></a>';
}
?>
</div>
</div>
/div><!-- /.box -->
</section><!-- right col -->
This is my first question so please don't be to hard on me
Paging by SQL can be implemented with the new features on ORDER BY clause like Offset and Fetch Next
Here is a sample query
SELECT * FROM TableName ORDER BY ID OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY
If you parameterize the Offset count like (page number * page row count) you can build a paging structure for your web page
I need tips or direction on how can I display data from mysql using echo. But I want to display it in html code. I want to display $row["title"] of first title in mysql instead title1 and $row["content"] of first content in mysql instead content1 and do that for all 3 divs. php code works fine I just can't figure out how to make that possible.
<div class="carousel-inner" style="background-size:cover;">
<div class="item active">
<img src="img/road1.jpg">
<div class="carousel-caption d-none d-md-block">
<h2>title1</h2>
<p>content1</p>
</div>
</div>
<div class="item">
<img src="img/road2.jpg">
<div class="carousel-caption d-none d-md-block">
<h2>title2</h2>
<p>content2</p>
</div>
</div>
<div class="item">
<img src="img/road3.jpg">
<div class="carousel-caption d-none d-md-block">
<h2>title3</h2>
<p>content3</p>
</div>
</div>-->
<?php
session_start();
include_once("db.php");
$sql = "SELECT * FROM news";
$query = mysqli_query($conn, $sql);
if (mysqli_num_rows($query) > 0) {
while($row = mysqli_fetch_assoc($query)) {
echo "<h2>" . $row["title"] . "</h2>";
echo "<p>" . $row["content"] . "</p>";
}
} else {
echo "0 results";
}
?>
You're almost there. Just move the html into the echo of the while loop.
echo '<div class="carousel-inner" style="background-size:cover;">';
$counter = 1;
while($row = mysqli_fetch_assoc($query)) {
echo '
<div class="item ' . ($counter == 1 ? 'active' : '') . '">
<img src="img/road{$counter}.jpg">
<div class="carousel-caption d-none d-md-block">
<h2>' . $row["title"] . '</h2>
<p>' . $row["content"] . '</p>
</div>
</div>';
$counter++;
}
echo '</div>';
The only issue is the image, realistically you'd save the image in the database with the title and content then use the same method but for this simple case lets just use a counter
please note that I change your entire code a little bit to make the desired results...
<div class="carousel-inner" style="background-size:cover;">
<?php
session_start();
include_once("db.php");
$sql = "SELECT * FROM news";
$query = mysqli_query($conn, $sql);
if (mysqli_num_rows($query) > 0) {
while($row = mysqli_fetch_assoc($query)) { ?>
<div class="item active">
<img src="img/road1.jpg">
<div class="carousel-caption d-none d-md-block">
<h2><?php echo $row["title"]; ?></h2>
<p><?php echo $row["content"]; ?></p>
</div>
</div>
<?php
}
} else {
echo "0 results";
}
?>
Also note that I'm repeating just the first image... You need an extra on planning to determine how to handle images in code and then update this one.
How can I loop results to insert new div with complexities for each result?
I have the following database table with following columns:
ID | UserID | Title | Introduction | Content | Images | Background | Date | Pinned
I have the following PHP code:
if($latest = $con->query("SELECT * FROM Posts WHERE Pinned='0' LIMIT 4")) {
if($latest->num_rows > 0) {
//<-- result loop here
} else {
echo '<h1 class="alert fade">No Posts</h1>';
}
$latest->close();
};
I would like to format the output as follows:
<div class="post standard" style="background-image:url([1]);">
<a href="view.php?id=[2]">
<div class="shader"></div>
<div class="info">
<h1>[3]</h1>
<p>[4] - [5]</p>
</div>
</div>
[1] - Background
[2] - ID
[3] - Title
[4] - UserID
[5] - Date
How would I accomplish this?
Here is the code which should do what you need. I used just one echo, you can split it into more echoes, or go out of PHP block. Doesn't matter.
<?php
if ($latest->num_rows > 0) {
while ($row = $latest->fetch_assoc()) {
echo '
<div class="post standard" style="background-image:url(' . $row['background'] . ');">
<a href="view.php?id=' . $row['id'] . '">
<div class="shader"></div>
<div class="info">
<h1>' . $row['title'] . '</h1>
<p>' . $row['userID'] . ' - ' . $row['date'] . '</p>
</div>
</div>
';
}
}
?>
Example with putting HTML code outside PHP block.
<?php
if ($latest->num_rows > 0) {
while ($row = $latest->fetch_assoc()) {
?>
<div class="post standard" style="background-image:url('<?php echo $row['background']; ?>');">
<a href="view.php?id=<?php echo $row['id'] ?>">
<div class="shader"></div>
<div class="info">
<h1><?php echo $row['title']; ?></h1>
<p><?php echo $row['userID']; ?> - <?php echo $row['date']; ?></p>
</div>
</div>
<?php
}
}
?>
Well, what I do is the following
let's say that we're inside the loop for example
if($latest = $con->query("SELECT * FROM Posts WHERE Pinned='0' LIMIT 4")) {
if($latest->num_rows > 0) {
//<-- result loop here
// end the php tag here
?>
<div></div>
<!-- you html tags here-->
<?php // reopen your php tag here again
} else {
echo '<h1 class="alert fade">No Posts</h1>';
}
$latest->close();
};
I'm having troubles displaying results into different divs.
I have this html code:
<div class="grid_10 alpha omega">
<div class="grid_5 alpha paddingtop10">
<div class="grid_5" id="titlescontent"><p class="titlebar">Subcat 1</p></div>
<div class="clear"></div>
<div class="grid_5" id="content"><p>Subsubcategories 1</p></div>
<div class="clear"></div>
</div>
<div class="grid_5 omega paddingtop10">
<div class="grid_5" id="titlescontent"><p class="titlebar">Subcat 2</p></div>
<div class="clear"></div>
<div class="grid_5" id="content"><p>Subsubcategories 2</p></div>
<div class="clear"></div>
</div>
<div class="grid_5 alpha paddingtop10">
<div class="grid_5" id="titlescontent"><p class="titlebar">Subcat 3</p></div>
<div class="clear"></div>
<div class="grid_5" id="content"><p>Subsubcategories 3</p></div>
<div class="clear"></div>
</div>
<div class="grid_5 omega paddingtop10">
<div class="grid_5" id="titlescontent"><p class="titlebar">Subcat 4</p></div>
<div class="clear"></div>
<div class="grid_5" id="content"><p>Subsubcategories 4</p></div>
<div class="clear"></div>
</div>
etc ...
</div>
<div class="clear"></div>
I'm using the 960grid system and alpha class is clearing 5px padding to the left while omega is clearing 5px padding to the right. The other classes are just for colors except of paddingtop10.
This is my php code structure I was trying to implement into my html:
if ($stmt = mysqli_prepare($connect, "SELECT subcategories.subcat_name, subsubcategories.subsubcat_name, subcategories.subcat_ID FROM subcategories INNER JOIN subsubcategories ON subcategories.subcat_ID=subsubcategories.subcat_ID WHERE subcategories.cat_ID = ? OR subcategories.extra_cat_ID = ? ORDER BY subcategories.subcat_name, subsubcategories.subsubcat_name ASC")){
mysqli_stmt_bind_param($stmt, "ii", $cat_ID, $cat_ID);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $subcat_name, $subsubcat_name, $subcat_ID);
$lastcat = 0;
while (mysqli_stmt_fetch($stmt)){
if($lastcat != $subcat_ID){
$lastcat = $subcat_ID;
echo "<br>";
echo $subcat_name;
echo "<br>";
echo "<br>";
}
echo $subsubcat_name;
echo "<br>";
}
}
I want to generate the alpha and omega divs through php but I'm not sure how to do this.
I tried this code but it doesn't give me the right result like I want shown in my html example code:
if ($stmt = mysqli_prepare($connect, "SELECT subcategories.subcat_name, subsubcategories.subsubcat_name, subcategories.subcat_ID FROM subcategories INNER JOIN subsubcategories ON subcategories.subcat_ID=subsubcategories.subcat_ID WHERE subcategories.cat_ID = ? OR subcategories.extra_cat_ID = ? ORDER BY subcategories.subcat_name, subsubcategories.subsubcat_name ASC")){
mysqli_stmt_bind_param($stmt, "ii", $cat_ID, $cat_ID);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $subcat_name, $subsubcat_name, $subcat_ID);
$lastsubcat = 0;
while (mysqli_stmt_fetch($stmt)){
echo '<div class="grid_5 alpha paddingtop10">';
echo '<div class="grid_5" id="titlescontent"><p class="titlebar">';
if($lastsubcat != $subcat_ID){
$lastsubcat = $subcat_ID;
echo $subcat_name;
}
echo '</p></div>';
echo '<div class="clear"></div>';
echo '<div class="grid_5" id="content"><p>';
echo $subsubcat_name;
echo '</p></div>';
echo '<div class="clear"></div>';
echo '</div>';
echo '<div class="grid_5 omega paddingtop10">';
echo '<div class="grid_5" id="titlescontent"><p class="titlebar">';
if($lastsubcat != $subcat_ID){
$lastsubcat = $subcat_ID;
echo $subcat_name;
}
echo '</p></div>';
echo '<div class="clear"></div>';
echo '<div class="grid_5" id="content"><p>';
echo $subsubcat_name;
echo '</p></div>';
echo '<div class="clear"></div>';
echo '</div>';
}
}
Any idea how I need to make this work?
Thank you for any help!
The only thing I can think of is to use a counter to track when you're in the first or last iteration. This would work even if the loop has only one item.
$values = mysqli_stmt_fetch($stmt);
$i = 0;
$len = count($values);
while ($values) {
$i++;
...
if ($i == 1) {
// add alpha class
}
...
if ($i == $len) {
// add omega class
}
}
Does that spark any ideas for you?