I'm trying to display text from a database row into 2 different divs.
The code currently selects all text from the row and displays both pieces of text in each div. Which isn't what I need. Is there a way to split the text, and have each text in its own div?
PHP:
//output each row
while ( $row = $result->fetch_assoc() ) {
echo $row["content"];
}
HTML:
//Text box 1
<div>
<p id="Text1">
<?php echo $row["content"]; ?>
</p>
</div>
//Text box 2
<div>
<p id="Text2">
<?php echo $row["content]; ?>
</p>
</div>
Thank you.
Just put the relevant HTML inside the loop:
$i = 1; // For the id's
while ( $row = $result->fetch_assoc() ) {
?>
<div>
<p id="Text<?= $i++; ?>">
<?= $row["content"]; ?>
</p>
</div>
<?php
}
//Do this
while ( $row = $result->fetch_assoc() ) {
$column1 .= '<p>'.$row["column1"].'</p>';
$column2 .= '<p>'.$row["column2"].'</p>';
$column3 .= '<p>'.$row["column3"].'</p>';
$column4 .= '<p>'.$row["column4"].'</p>';
//and so on...
}
//Column 1
<div id="column1">
<?php echo $column1; ?>
</div>
//Column 2
<div id="column2">
<?php echo $column2; ?>
</div>
//Column 3
<div id="column3">
<?php echo $column3; ?>
</div>
//Column 4
<div id="column4">
<?php echo $column4; ?>
</div>
//and so on...
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 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 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>';
}
I have a problem with my search.php file to render me results...
I dont get any strings of error, but when I type an existing keyword I get no results...
The format of the results are the same as viewed in my main content on the website (grid view)...
The code:
<body>
<?php include_once("analyticstracking.php") ?>
<div class='container'> <!--Start of the container-->
<div><?php include("includes/header.php"); ?></div>
<div><?php include("includes/navbar.php"); ?></div>
<div><?php include("includes/left_col.php"); ?></div>
<div class='main_col'>
<div class='main_content'>
<?php
include("includes/connect.php");
if(isset($_GET['search'])){
$search_id = $_GET['q'];
$search_query = "SELECT * FROM games WHERE game_keywords LIKE '%$search_id%'";
$run_query = mysql_query($search_query);
echo '<table>';
$games = 0;
while($search_row = mysql_fetch_array($run_query)){
// make a new row after 9 games
if($games%9 == 0) {
if($games > 0) {
// and close the previous row only if it's not the first
echo '</tr>';
}
echo '<tr>';
}
// make a new column after 3 games
if($games%3 == 0) {
if($games > 0) {
// and only close it if it's not the first game
echo '</td>';
}
echo '<td>';
}
$game_id = $search_row['game_id'];
$game_name = $search_row['game_name'];
$game_category = $search_row['game_name'];
$game_keywords = $search_row['game_name'];
$game_image = $search_row['game_image'];
?>
<div class="game_grid">
<a href="game_page.php?id=<?php echo $game_id; ?>"><img src="images/games_images/<?php echo $game_image; ?>" width="120" height="120" />
<span><?php echo $game_name; ?></span>
</div>
<?php
$games++;
}
}
?>
</table>
</div>
</div>
<div><?php include("includes/footer.php"); ?></div>
</div> <!--End of the container-->
</body>
Any idea?
EDIT:
I solved my problem, its a small mistake I made,
In the HTML form of the search I forgot to give the submit button: "name="search", I removed it accidently... now everything works perfectly :)
You have a typo in code
change code as below
if(isset($_GET['search'])){
$search_id = $_GET['search']; //$_GET['q'];
.
.
.
}
I solved my problem, its a small mistake I made, In the HTML form of the search I forgot to give the submit button: "name="search", I removed it accidentally... now everything works perfectly :)
How could I find the last post and NOT put <hr> below it?
<?php foreach($db->query("SELECT * FROM news") as $row): ?>
<div class="NewsHeadline">
<div class="NewsDate"><?php echo $date; ?> - </div>
<div class="NewsTitle"><?php echo $title; ?></div>
</div>
<div class="NewsBody">
<?php echo $body; ?>
</div>
<hr>
<?php endforeach ?>
Set a counter equal to 1 which increments each round, and check if it is equal to the count() of the $query array returned. If it is, you are on the last round!
<?php
$counter = 1;
$total = count($db->query("SELECT * FROM news"));
?>
<?php foreach($db->query("SELECT * FROM news") as $row): ?>
<div class="NewsHeadline">
<div class="NewsDate"><?php echo $date; ?> - </div>
<div class="NewsTitle"><?php echo $title; ?></div>
</div>
<div class="NewsBody">
<?php echo $body; ?>
</div>
<?php if($counter != $total){
?>
<hr>
<?php
}
?>
<?php $counter++; ?>
<?php endforeach ?>
A solution would be to build an array and then use join :
<?php
$arr=array();
foreach($db->query("SELECT * FROM news") as $row):
$arr[] = '<div class="NewsHeadline"><div class="NewsDate">'.$date
.' - </div><div class="NewsTitle">'.$title'
.</div></div><div class="NewsBody">'.$body.'</div>';
endforeach
echo join("<hr>",$arr);
?>
You can convert to regular for loop and then compare $i to the size of your query result:
<?php
$news = $db->query("SELECT * FROM news");
for($i=0; $i<sizeof($news);$i++){ ?>
<div class="NewsHeadline">
<div class="NewsDate"><?php echo $date; ?> - </div>
<div class="NewsTitle"><?php echo $title; ?></div>
</div>
<div class="NewsBody">
<?php echo $body; ?>
</div>
<?php if(($i+1) < sizeof($news)) echo '<hr>'; ?>
<?php } ?>
If you get a count of the rows prior to the loop, you can check the count and avoid adding the HR.
$rows = $db->query("SELECT * FROM news");
$row_count = $rows.count();
$iter = 0;
foreach($rows as $row)
{
//...
if ($iter < $row_count)
{
// render hr
}
$iter++;
}
You could also go with a pure CSS approach:
div.containerClass:last-child hr { display:none; }
where "containerClass" would be whatever element your posted code sits inside of.