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();
};
Related
I have data in my phpmyadmin database table. And I want to display them in a page where the data will be displayed in 4 columns and move to another row for more data.
For example
1 2 3 4
5 6 7 8
I also want it to be responsive. I am using html and php in Sublime text software.
I am developing a website for restaurant and having problem displaying menu details(images, price, desc, etc) in a page using table format. Currently, my data are displaying in the one row and not responsive at all.
<?php
echo "<tr>";
$subselect="SELECT * FROM menu ";
$subret=mysqli_query($connection,$subselect);
$subcount=mysqli_num_rows($subret);
for($j=0;$j<$subcount;$j++)
{
$row=mysqli_fetch_array($subret);
$MenuName = $row['MenuName'];
$MenuDesc = $row['Description'];
$MenuPrice = $row['Price'];
$MenuImage="MenuImage/" . "_" . $row['MenuImage'];
list($width, $height, $type, $attr)=getimagesize($MenuImage);
$w=200;
$h=200;
echo "<td align='center'>";
?>
<section class="ftco-section"> //this is for the display section
<div class="container">
<div class="blog-entry align-self-stretch">
<img src="<?php echo $MenuImage ?>"width="<?php echo $w ?>" height="<?php echo $h ?>">
<div class="text">
<h3><?php echo $MenuName ?></h3>
<p ><?php echo $MenuDesc ?></p>
<p class="price"><span><?php echo $MenuPrice ?></span> mmk </p>
Add to cart
</div>
</div>
</div>
</div>
</section>
<?php
}
?>
I want my data to be displayed like
1 2 3 4
5 6 7 8
Please try the below code:
<style>
.text{
padding:10px;
float:left;
}
.clear{
clear:both;
}
</style>
<?php
echo "<table>";
$subret = [1,2,3,4,5,6,7,8];
$loop_counter = $Break = 1;
$subcount=count($subret);
for($j=0;$j<$subcount;$j++)
{
$row=$age= array("MenuName"=>"Name","Description"=>"Dec","Price"=>"43");
$MenuName = $row['MenuName'].'_'.$subret[$j];
$MenuDesc = $row['Description'].'_'.$subret[$j];
$MenuPrice = $row['Price'].'_'.$subret[$j];
if($Break == 1){
?>
<tr><td align='center'>
<section class="ftco-section">
<div class="container">
<div class="blog-entry align-self-stretch">
<?php
}
?>
<div class="text">
<h3><?php echo $MenuName ?></h3>
<p ><?php echo $MenuDesc ?></p>
<p class="price"><span><?php echo $MenuPrice ?></span> mmk </p>
Add to cart
</div>
<?php
if($loop_counter%4==0){ echo '</div></div></section></td></tr><div class="clear"></div>'; $Break = 1;}else{$Break = 0;}
$loop_counter++;
}
echo "</table></tr>";
?>
Result:
I have a working code that can insert and update information from the database and echoing it to page. but I like to hide the specific empty column while displaying all the information from the row. check this screenshot
my goal is to hide the "image" column when its null/empty; so the crock image wont display. here is my code below:
<?php
$result = mysqli_query($mysqli, "SELECT * FROM blogs ORDER BY id DESC");
while ($row = mysqli_fetch_array($result))
if (!empty($row['image'] != ""))
{
echo "<div class='row'>
<div class='col-lg-12 box'>
<div class='content-heading'>
<p>
<text>".$row['title']."</text>
</p>
</div>
<p>
<text1 class='pull-right' >".$row['image_text']."</text1><br/>
<img class='img-size' id='hp'src='admin/upload_images/".$row['image']."'/>
<text>".$row['definition']."</text>
</p>
</div>
</div>";
}
?>
but this code if (!empty($row['image'] != "")) is hiding the entire row from my database.
Can anyone have a right solution to my problem?
You could try using the style attribute of the <img> tag to selectively show or hide the image tag:
<img class='img-size'
id='hp'
style='display: '. ($row["image"] != "" ? "block" : "none") . ';'
src='admin/upload_images/".$row['image']."'/>
Instead of hiding entire row, you could just hide the image tag, like this:
<?php
$result = mysqli_query($mysqli, "SELECT * FROM blogs ORDER BY id DESC");
while ($row = mysqli_fetch_array($result))
{
echo "
<div class='row'>
<div class='col-lg-12 box'>
<div class='content-heading'>
<p>
<text>".$row['title']."</text>
</p>
</div>
<p>
<text1 class='pull-right' >".$row['image_text']."</text1><br/>
";
if (!empty($row['image'])) {
echo "<img class='img-size' id='hp'src='admin/upload_images/".$row['image']."'/>";
}
echo "
<text>".$row['definition']."</text>
</p>
</div>
</div>
";
}
?>
Your if condition will hide all other html elements as well instead you should hide only img tag when no value found for image.
Update your code as below :
while ($row = mysqli_fetch_array($result)) {
$image = (!empty($row['image'])) ? "<img class='img-size' id='hp'src='admin/upload_images/" . $row['image'] . "'/>" : "";
echo "<div class='row'>
<div class='col-lg-12 box'>
<div class='content-heading'>
<p>
<text>" . $row['title'] . "</text>
</p>
</div>
<p>
<text1 class='pull-right' >" . $row['image_text'] . "</text1><br/>
" . $image . "
<text>" . $row['definition'] . "</text>
</p>
</div>
</div>";
}
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.
I've been searching through the forums and Google for a few days on how to resolve this and have yet to find a solution. Any assistance would be greatly appreciated.
Bottom line, I am looking to achieve this:
Speaker Section
Using Firebug to inspect the element, I've determined which classes apply:
<div class="et_pb_section et_pb_section_8 et_pb_with_background et_section_regular">
<div class=" et_pb_row et_pb_row_6">
<div class="et_pb_column et_pb_column_4_4 et_pb_column_10">
<div class="et_pb_blog_grid_wrapper">
<div class="et_pb_blog_grid clearfix et_pb_module et_pb_bg_layout_ et_pb_cpt_loop_archive_0 et_pb_cpt_archive_grid" data-columns="">
<div class="et_pb_row et_pb_row_cpt et_pb_row_4col">
<div class="et_cpt_container_column et_pb_column et_pb_column_1_4 et_pb_column_0">
<div class="et_pb_section et_pb_section_10 et_section_regular">
<div class=" et_pb_row et_pb_row_7">
<div class="et_pb_column et_pb_column_4_4 et_pb_column_11">
<div class="et_pb_module et-waypoint et_pb_image et_pb_animation_off desaturate et_pb_cpt_featured_image2_0 et_always_center_on_mobile et-animated">
<a href="http://meetingoftheminds.org/speaker/scot-rourke"><img src="http://meetingoftheminds.org/wp-content/uploads/2017/03/Scot-Rourke.jpg" alt="">
</a>
</div>
<div class="clearfix et_pb_module et_pb_bg_layout_light et_pb_text_align_left et_pb_cpt_title_0">
<h2 itemprop="name" class="cpt_title page_title entry-title">Scot Rourke</h2>
</div>
<div class="clearfix et_pb_module et_pb_acf_single_item_0">
<div class="sb_mod_acf_single_item clearfix">
<p>Chief Information and Transformation Officer, Technology, Innovation & Performance</p>
</div>
</div>
<div class="clearfix et_pb_module et_pb_acf_single_item_1">
<div class="sb_mod_acf_single_item clearfix">
<p>Cuyahoga County</p>
</div>
</div>
</div>
<!-- .et_pb_column -->
</div>
<!-- .et_pb_row -->
</div>
<!-- .et_pb_section -->
</div>
I then tried to tailor this to my PHP Loop but the result is radically different than expected.
Here is the snippet of the original loop:
<?php
// Get the connected posts
$my_connected_posts = Post_Connector::API()->get_children( "sessions-to-speakers", get_the_id() );
// Check
if ( count( $my_connected_posts ) > 0 ) {
// Loop
foreach ( $my_connected_posts as $my_connected_post ) {
// Display the featured image, title with link, job, and company
echo get_the_post_thumbnail( $my_connected_post, 'thumbnail') . "<br/>";
echo "<a href='" . get_permalink( $my_connected_post->ID ) . "'>" . $my_connected_post->post_title . "</a>" . "<br/>";
echo get_field("job", $my_connected_post) . "<br/>";
echo get_field("company", $my_connected_post) . "<br/>";
}
}
?>
Original loop displays posts vertically down the page as shown here:
Connected Posts
Here is the snippet of the modified loop, using the "Speaker Section" classes, which only seems to resize the thumbnails:
<!-- Speaker Section -->
<div class="et_pb_section et_pb_section_8 et_pb_with_background et_section_regular">
<div class=" et_pb_row et_pb_row_6">
<div class="et_pb_column et_pb_column_4_4 et_pb_column_10">
<div class="et_pb_blog_grid_wrapper">
<div class="et_pb_blog_grid clearfix et_pb_module et_pb_bg_layout_ et_pb_cpt_loop_archive_0 et_pb_cpt_archive_grid" data-columns="">
<div class="et_pb_row et_pb_row_cpt et_pb_row_4col">
<?php
// Get the connected posts
$my_connected_posts = Post_Connector::API()->get_children( "sessions-to-speakers", get_the_id() );
// Check
if ( count( $my_connected_posts ) > 0 ) {
// Loop
foreach ( $my_connected_posts as $my_connected_post ) {
// #speaker section
echo '<div class="et_cpt_container_column et_pb_column et_pb_column_1_4 et_pb_column_0"><div class="et_pb_section et_pb_section_10 et_section_regular">';
echo '<div class=" et_pb_row et_pb_row_7">';
echo '<div class="et_pb_column et_pb_column_4_4 et_pb_column_11">';
// Display the featured image, title with link, job, and company
echo '<div class="et_pb_module et-waypoint et_pb_image et_pb_animation_off desaturate et_pb_cpt_featured_image2_0 et_always_center_on_mobile et-animated">';
echo get_the_post_thumbnail( $my_connected_post, 'thumbnail') . "<br/>";
echo '</div>';
echo '<div class="clearfix et_pb_module et_pb_bg_layout_light et_pb_text_align_left et_pb_cpt_title_0">';
echo "<a href='" . get_permalink( $my_connected_post->ID ) . "'>" . $my_connected_post->post_title . "</a>" . "<br/>";
echo '</div>';
echo '<div class="clearfix et_pb_module et_pb_acf_single_item_0">';
echo '<div class="sb_mod_acf_single_item clearfix">';
echo get_field("job", $my_connected_post) . "<br/>";
echo '</div>';
echo '</div>';
echo '<div class="clearfix et_pb_module et_pb_acf_single_item_1">';
echo '<div class="sb_mod_acf_single_item clearfix">';
echo get_field("company", $my_connected_post) . "<br/>";
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
}
}
?>
I have a list of content that I would have a paging. Until now I was able to count correctly how content we are, then we have 3 content, and imposed a maximum of 2 applications per page, in my index shows the link to go to the next page.
Unfortunately, however, always displays the same content. For example:
http://i62.tinypic.com/156skjo.png
http://i60.tinypic.com/30xcy1l.png
However, I paste the current code, I hope you can help me out. Thank You
<?php require_once 'app/init.php'; ?>
<?php echo View::make('header')->render() ?>
<div class="row">
<!-- START MAIN GRID -->
<?php
// Create a variable imposed where the number of records
// To display on each page
$x_pag = 2;
// Retrieve the current page number.
// Usually you use a querystring
$pag = isset($_GET['pag']) ? $_GET['pag'] : 1;
// Check if $pag is valued and if numeric
// ... Otherwise I assign a value of 1
if (!$pag || !is_numeric($pag)) $pag = 1;
$quest = DB::table('questions')
->count();
// Using a simple mathematical operation define the total number of pages
$all_pages = ceil($quest / $x_pag);
// Calculation of which record start
$first = ($pag - 1) * $x_pag;
$questions = DB::table('questions')
->orderBy('id', 'desc')
->take($x_pag)
->get();
foreach ($questions as $question):
$user = User::find($question->user_id);
?>
<div class="col-md-4 col-lg-4">
<div class="main-grid">
<div class="profile-inner img-responsive" style="background-image: url('images/<?php echo $question->h_image; ?>');border-radius: 10px 10px 0px 0px;">
<img src="<?php echo $user->avatar; ?>" class="small-thumb" >
</div>
<div class="description">
<h5><strong><?php echo $question->user_name; ?></strong>
<?php if (Auth::check() && Auth::user()->id != $question->user_id): ?>
<?php $contact = Contact::find(Auth::user()->id, $question->user_id); ?>
<?php if (!empty($contact) && !empty($contact->accepted)): ?>
( <?php _e('main.remove_contact') ?> )
<?php elseif (!empty($contact)): ?>
( <a href="javascript:EasyLogin.removeContact(<?php echo $question->user_id ?>)" data-contact-id="<?php echo $question->user_id ?>" ><?php _e('main.cancel_contact') ?></a> )
<?php else: ?>
( <?php _e('main.add_contact') ?> )
<?php endif ?>
<?php endif ?>
</h5>
<h3><?php echo $question->h_title; ?>?</h3>
<hr />
Help +
</div>
</div>
</div>
<?php endforeach; ?>
<!-- MAIN GRID END -->
</div>
<?php
// If the total pages are more than 1 ...
// Mold the link to go back and forth between different pages!
if ($all_pages > 1){
if ($pag > 1){
echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?pag=" . ($pag - 1) . "\">";
echo "Back</a> ";
}
if ($all_pages > $pag){
echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?pag=" . ($pag + 1) . "\">";
echo "Next</a>";
}
}
?>
<?php echo View::make('footer')->render() ?>