I've got this foreach script and if there is no result found it does show this message "No articles yet" twice, how can i fix this? I've tried to move it out of the foreach but with no success.
Many thanks
$query_show_groupmsg = mysqli_prepare($conn, "SELECT group_message_id, group_message_date, group_message_subject, group_message_body FROM group_messages WHERE group_message_refid = ? ORDER BY group_message_date DESC LIMIT 5");
mysqli_stmt_bind_param($query_show_groupmsg, 'i', $data);
foreach ($id_gruppo as $data) {
mysqli_stmt_execute($query_show_groupmsg);
mysqli_stmt_bind_result($query_show_groupmsg, $group_message_id, $group_message_date, $group_message_subject, $group_message_body);
mysqli_stmt_store_result($query_show_groupmsg);
$groupmsg = mysqli_stmt_num_rows($query_show_groupmsg);
if ($groupmsg > 0) {
while (mysqli_stmt_fetch($query_show_groupmsg)) {
$new_mgsgroup_date = date('d/m/Y, \a\l\l\e h:i a', strtotime($group_message_date));
?>
<div class="item">
<div class="item-head">
<div class="item-details">
<?php
echo ' <a class="item-name primary-link" href="client_blog_app/client_group_post_view.php?id='.$group_message_id.'"> '.ucfirst(strip_tags(mb_strimwidth($group_message_subject, 0, 350, "..."))).'</a>
';
?>
<br><span class="item-label font-blue small">Pubblicato il <?php echo "$new_mgsgroup_date"; ?></span>
</div>
</div>
<div class="item-body"> <?php echo strip_tags(mb_strimwidth($group_message_body, 0, 350,"...")); ?> </div>
</div>
<?php
}
}else{
echo '
<div class="alert alert-info">No articles yet</div>
';
}
}
Add this outside of the for loop and remove the else clause you already have.
<?
if ($groupmsg < 1)
{
?>
<div class="alert alert-info">No articles yet</div>
<?
}
Put the IF ELSE statement outside the FOREACH like this:
$query_show_groupmsg = mysqli_prepare($conn, "
SELECT group_message_id, group_message_date, group_message_subject, group_message_body FROM group_messages
WHERE group_message_refid = ?
ORDER BY group_message_date
DESC LIMIT 5
");
$article_count = 0;
mysqli_stmt_bind_param($query_show_groupmsg, 'i', $data);
foreach ($id_gruppo as $data) {
mysqli_stmt_execute($query_show_groupmsg);
mysqli_stmt_bind_result($query_show_groupmsg, $group_message_id, $group_message_date, $group_message_subject, $group_message_body);
mysqli_stmt_store_result($query_show_groupmsg);
$groupmsg = mysqli_stmt_num_rows($query_show_groupmsg);
if ($groupmsg > 0) {
while (mysqli_stmt_fetch($query_show_groupmsg)) {
$new_mgsgroup_date = date('d/m/Y, \a\l\l\e h:i a', strtotime($group_message_date));
$article_count++;
?>
<div class="item">
<div class="item-head">
<div class="item-details">
<?php echo ' <a class="item-name primary-link" href="client_blog_app/client_group_post_view.php?id='.$group_message_id.'"> '.ucfirst(strip_tags(mb_strimwidth($group_message_subject, 0, 350, "..."))).'</a>'; ?>
<br>
<span class="item-label font-blue small">Pubblicato il <?php echo "$new_mgsgroup_date"; ?></span>
</div>
</div>
<div class="item-body"> <?php echo strip_tags(mb_strimwidth($group_message_body, 0, 350,"...")); ?> </div>
</div>
<?php
}
}
}
if ($article_count == 0){
echo '<div class="alert alert-info">No articles yet</div>';
}
Since your if ($groupmsg > 0) condition is inside the foreach cycle, the else branch can be executed up to count($data) times. Moving the condition out of the foreach loop should fix this. You can keep count of articles inside foreach and after the foreach check if there were any.
However this code is really terrible to read (no comments, random indentation). You should make a minimal working example, remove the useless parts and comment your code, if needed.
Related
I have the following data and would like to display it in different containers in html.
Name Price Difference Signal
CA.PA 15.85 3.5609257364073 MACD
AZN.ST 896 3.4881049471963 MACD
AMGN 258.57 1.6391533819031 SMA 50/200
The containers are winner_1. As of right now the first winner_1 display the last Name from the above table.
How can I get it to say CA.PA in the first winner_1, and AZN.ST in the second winner_1, and AMGN in the last winner_1.
<div class="overview">
<h1>Winners</h1>
<div class="winner">
<?php
foreach ($res_winners_weekly as $r){
$name = $r["Name"];
$Price = $r['Price'];
$percent_diff = $r['Difference'];
$signal = $r['Signal'];
}
?>
<div class="winner_1">
<?php echo $name; ?>
</div>
<div class="winner_1">
<?php echo $name +1; ?>
</div>
<div class="winner_1">
</div>
</div>
</div>
The page can be seen here:
https://signal-invest.com/markets-today/
One option is generated div tags using php:
<div class="overview">
<h1>Winners</h1>
<div class="winner">
<?php
foreach ($res_winners_weekly as $r) {
$name = $r["Name"];
$Price = $r['Price'];
$percent_diff = $r['Difference'];
$signal = $r['Signal'];
echo "<div class='winner_1'>";
echo "<a href='#'>{$name}</a>";
echo "</div>";
}
?>
</div>
</div>
You should see following logic and try doing this way. Hopefully your problem will be resolved.
<div class = "overview">
<h1>Winners</h1>
<div class = "winner">
<?php
foreach ($res_winners_weekly as $r) {
$name = $r["Name"];
$Price = $r['Price'];
$percent_diff = $r['Difference'];
$signal = $r['Signal'];
echo "<div class='winner_1'><a href='#'> $name </a></div>";
}
?>
</div>
</div>
I have used foreach loop to display the rows in myphp database, however I would now like to display this in DESC order (display last row first) however research tells me not to use the SELECT command in arrays? is there another way? Many thanks.
index.php:
<?php
include("app/database/db.php");
$posts = selectAll('posts', ['published' => 1]);
?>
<?php foreach ($posts as $post): ?>
<div class="post">
<div class="col-lg-6">
<!-- HEADLINE -->
<h1><strong><?php echo $post['title']; ?></strong></h1>
<!-- TEXT BODY -->
<!-- <h4></?php echo $post['body']; ?></h4> displays the full body -->
<h4 class="mainbody"><?php echo html_entity_decode(substr($post['body'], 0, 100) . '...'); ?></h4>
<!-- IMAGE -->
<div class="col-lg-6">
<img src="<?php echo '/assets/images/' , $post['image']; ?>" style="width: 550px; height: 350px; object-fit: cover;">
</div>
</div>
<?php endforeach; ?>
db.php file:
<?php
function selectAll($table, $conditions = []) {
global $conn;
$sql = "SELECT * FROM $table";
if (empty($conditions)) {
$stmt = $conn->prepare($sql);
$stmt->execute();
$records = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
return $records;
} else {
$i = 0;
foreach ($conditions as $key => $value) {
if ($i === 0) {
$sql = $sql . " WHERE $key=?";
} else {
$sql = $sql . " AND $key=?";
}
$i++;
}
$stmt = executeQuery($sql, $conditions);
$records = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
return $records;
}
}
?>
I need to wrap every 2 divs with another div for a project (a row) so it will look like:
<div class="row">
<div> Item </div>
<div> Item </div>
</div>
<div class="row">
<div> Item </div>
<div> Item </div>
</div>
I have tried a few solutions but they dont work since the items coming in are odd (9 items). Here is what I had:
<?php
$count = 0;
foreach ($contents as $content)
{
//var_dump($content);
$books = $content["tags"];
$book_image = $content['content_image'];
$book_desc = $content['content_social_description'];
++$count;
if($count == 1)
{
echo "<div class='et_pb_row'>";
}
foreach ($books as $book)
{
$book_name = $book['tag_name'];
$book_name_trim = str_replace(' ', '-', $book_name);
?>
<!-- Inside the Book Loop -->
<div class='et_pb_column et_pb_column_1_2 books' style="background: url('https://s3-us-west-2.amazonaws.com/crowdhubproverbs31/<?php echo $book_image ;?>');">
<h2><?php echo $book_name; ?></h2>
<p><?php echo $book_desc; ?></p>
<?php echo $count; ?>
</div>
<?php
}
if ($count == 2)
{
echo "</div>";
$count = 0;
}
}
?>
This works except the second to last row has 3 items even though it dispays the "count" as 2 when I echo it out so it should reset but doesnt. So its:
<div class="row">
<div> Item </div> "Count 1"
<div> Item </div> "Count 2"
</div>
<div class="row">
<div> Item </div> "Count 1"
<div> Item </div> "Count 2"
</div>
<div class="row">
<div> Item </div> "Count 1"
<div> Item </div> "Count 2"
<div> Item </div> "Count 2"
</div>
<div class="row">
<div> Item </div> "Count 1"
<div> Item </div> "Count 2"
</div>
You should open and close your <rows/> in the books loop, and add a late check for odd books:
<?php
$count = 0;
foreach ($contents as $content)
{
//var_dump($content);
$books = $content["tags"];
$book_image = $content['content_image'];
$book_desc = $content['content_social_description'];
foreach ($books as $book)
{
++$count;
if($count == 1)
{
echo "<div class='et_pb_row'>";
}
$book_name = $book['tag_name'];
$book_name_trim = str_replace(' ', '-', $book_name);
?>
<!-- Inside the Book Loop -->
<div class='et_pb_column et_pb_column_1_2 books' style="background: url('https://s3-us-west-2.amazonaws.com/crowdhubproverbs31/<?php echo $book_image ;?>');">
<h2><?php echo $book_name; ?></h2>
<p><?php echo $book_desc; ?></p>
<?php echo $count; ?>
</div>
<?php
if ($count == 2)
{
echo "</div>";
$count = 0;
}
}
}
if ($count > 0)
{
echo "</div>";
}
?>
Doing so, your $count variable will be incremented only when foreach($books AS $book) loop is run (thus you have at least one book to print)
You can take advantage of array_chunk :
//First make a generator to get all books
function allBooks($contents) {
foreach($contents as $content) {
foreach($content['tags'] as $book) {
yield $book; //Here you can yield whatever you want !
}
}
}
//Then create rows
$itemPerRow = 2;
$rows = array_chunk(iterator_to_array(allBooks($contents)), $itemPerRow, true);
//Display all
foreach($rows as $row) {
echo '<row>';
foreach($row as $book) {
//Display the book !
}
echo '</row>';
}
Overall I would like to echo all elements of the DB table 'products' as buttons on a row, and for each 10th element, a lineshift should be implemented. Each line should be surrounded by a specific ().
I have printed the code I have been working with to do this:
products_backend.php:
$stmt = $db->prepare("SELECT * FROM products");
$stmt->execute();
$count = $stmt->rowCount();
if($count > 1){
$table_row = $stmt->fetchAll();
$i = 0;
foreach($table_row as $data) {
$i++;
?>
<div class="col-md-1 cash_register pants">
<button class="table_add" value="Bukser">
<h4><?php echo $data['product_name']; ?></h4>
</button>
</div>
<?php
if ($i%10 == 0):
echo "<br>";
endif;
}
}
And then I have the "products.php page", where the extracted data is shown:
<div class="container-fluid">
<section class="wrapper">
<div class="row">
<div class="row-eq-height">
<?php require("products_backend.php"); ?>
</div>
</div>
</section>
</div>
But the output of this is all the products in the DB printed on one line, without any lineshift for every 10th line:
The image shows the output, however due to the window size, only 11 products are shown, but i keeps going outside the window.
To summarize, I want the output to insert a lineshift for every 10th product printed, and that each row should be inside the div, like in the manipulated Picture below: (Notice, that in order to create the Picture, I have just copied the output from the row line in order to create the second row, Thus I do not want the same output in the second row as in the first row)
So what am I doing wrong in my code above?
the class "col-md-1" looks like a floating div
so to break this floating divs, try this :
echo "<br style=\"clear : both;\">";
You can use % moduler to add div for after every 10 record;
Updated code :
$stmt = $db->prepare("SELECT * FROM products");
$stmt->execute();
$count = $stmt->rowCount();
if($count > 1){
$table_row = $stmt->fetchAll();
$i = 0;
foreach($table_row as $data) {
if(($i%10) == 0){ echo "<div class="clearfix"></div>";}
?>
<div class="col-md-1 cash_register pants">
<button class="table_add" value="Bukser">
<h4><?php echo $data['product_name']; ?></h4>
</button>
</div>
<?php
$i++;
}
}
Try this:
$stmt = $db->prepare("SELECT * FROM products");
$stmt->execute();
$count = $stmt->rowCount();
if($count > 1){
$table_row = $stmt->fetchAll();
$i = 1;
echo "<div>";
foreach($table_row as $data) {
?>
<div class="col-md-1 cash_register pants">
<button class="table_add" value="Bukser">
<h4><?php echo $data['product_name']; ?></h4>
</button>
</div>
<?php
if(($i%10) == 0){ echo "</div><div>";}
$i++;
}
echo "</div>";
}
Before i was displaying zones records static although i have records in database table
HTML
<div class="pub_fot_sec_menu pub_fot_list_fst">
<h2>Kosi</h2>
<h2>Mechi</h2>
<h2>Sagarmatha</h2>
</div>
<div class="pub_fot_sec_menu pub_fot_list_sec">
<h2>Bagmati</h2>
<h2>Janakpur</h2>
<h2>Narayani</h2>
</div>
<div class="pub_fot_sec_menu pub_fot_list_thrd">
<h2>Dhawalagiri</h2>
<h2>Gandaki</h2>
<h2>Lumbini</h2>
</div>
<div class="pub_fot_sec_menu pub_fot_list_frth">
<h2>Bheri</h2>
<h2>Karnali</h2>
<h2>Rapti</h2>
</div>
Now i want to fetch zone records using while or whatever method that work for me!
<?php
$sql="select * from tb_zone";
$res =mysql_query($sql);
while($data =mysql_fetch_array($res))
{
// want do display zone record in the above html output format
}
?>
Any help would be appreciated!
Try This
<?php
$class = array ('pub_fot_sec_menu pub_fot_list_fst','pub_fot_sec_menu pub_fot_list_sec','pub_fot_sec_menu pub_fot_list_thrd','pub_fot_sec_menu pub_fot_list_frth');
$sql="select * from tb_zone";
$res =mysql_query($sql);
$j=0;
$i=0;
while($data =mysql_fetch_array($res))
{
if($i==0)
{ echo '<div class="'.$class[$j].'">'; }
echo '<h2>'.$data["zone"].'</h2>';
if($i%2==0 && i > 0)
{ echo '</div>'; $j++;$i=0; }
else{ $i++; }
}
?>
just use this type
while($data =mysql_fetch_array($res))
{
echo
'
<div class="pub_fot_sec_menu pub_fot_list_fst">
<h2>'.$data['zone'].'</h2>
';
}