PHP putting the right row in the right html div - php

This one is going to be a bit hard to explain but I am going to try my best.
I have a database with a table called content with 3 columns, I’m trying to get the values as rows and put them in different <div>’s. here is what I wrote so far
<?php
$sql = "SELECT * FROM content";
$query = mysqli_query($db, $sql) or die (mysqli_error());
$contentDisplay = '';
while ($row = mysqli_fetch_array($query)) {
$content_id = $row["id"];
$content_title= $row["title"];
$content_text = $row["text"];
$contentDisplay .= '<h1>'.$content_title.'</h1> <p>'.$content_text.'</p>' ."\n";
}
$row_cnt = $query->num_rows;
printf("Result set has %d rows.\n", $row_cnt);
?>
The row count gives 4 rows which is correct amount of rows.
In my html I have <?php echo $contentDisplay; ?> which puts out all 4 rows after each other, but I need to show the first row in my first <div>, the second row in my second <div> and so forth. thanks in advance
Update: I forgot to say that I have my <div>'s in the html part with different styles. all i need is the first row in one <div> (like on top of the page) and second row in another <div> (like on the bottom of the page) :)
Update2: here is the html code
<div class="content-top">
<section>
<h1></h1>
<p></p>
</section>
</div>
<div class="content-left">
<section>
<h1></h1>
<p></p>
</section>
</div>
<div class="content-center">
<section>
<h1></h1>
<p></p>
</section>
</div>
<div class="content-right">
<section>
<h1></h1>
<p></p>
</section>
</div>
i want the first row to be displayed in <div class="content-top"> the second row in <div class="content-left"> the third row in <div class="content-center"> and the fourth row in <div class="content-right">

<?php
$sql = "SELECT * FROM content";
$query = mysqli_query($db, $sql) or die (mysqli_error());
$rows = [];
while ($row = mysqli_fetch_array($query))
$rows[] = '<h1>'.$row['title'].'</h1> <p>'.$row['text'].'</p>' . "\n";
?>
<div class="content-top">
<section>
<?php echo $rows[0]; ?>
</section>
</div>
<div class="content-left">
<section>
<?php echo $rows[1]; ?>
</section>
</div>
<div class="content-center">
<section>
<?php echo $rows[2]; ?>
</section>
</div>
<div class="content-right">
<section>
<?php echo $rows[3]; ?>
</section>
</div>

$contentDisplay .= '<div><h1>'.$content_title.'</h1> <p>'.$content_text.'</p></div>' ."\n";
Just add divs for each row? :) This way, each row in the database will get a div.
Each row will be appended to the $contentDisplay variable, but since all you are doing is printing it, you can print the html directly.
I'm not sure why you are being downvoted, since you are asking a valid question with code that shows what you have tried so far.

Related

issue when ifisset added to code

some explenation
http://koopjesidee.nl/images/6755185029021696.jpg
in my database i have a column named new_label
when its a new product its set as new
when no new product the colums is empty
i want the class="main-label new-label" to be disabled when there nothing in that column
now as you see from the image, there is always a orange cirkel (as set in class main-label) even when column is empty
This is my code
<!-- select products from database -->
<?php
$query = "select * from contentwhere cat = 4 order by rand() LIMIT 18";
$result = $db->query($query);
while($row = $result->fetch_array())
{
$url =detail($row);
<!-- content on main page -->
echo '
<div class="item">
<div class="product-item">
<div class="main-label new-label"><span>'.$row['new_label'].'</span></div>
<div class="product-image"><img src="'.$row['picture_big'].'" alt="'.$row['name'].'"></div>
<div class="product-item-details">
<div class="product-item-name"> '.$row['name'].' </div>
<div class="price-box"> <span class="price">€'.$row['price'].'</span> <del class="price old-price">'.$row['from_price'].'</del> </div>
</div>
</div>
</div>';
}?>
I want add if isset like this so when there is no text in class=new-label class main-label new-label is disabled column in my database is new_label
<div class="item">
<div class="product-item">
<!-- ISSUE -->
if (isset($_row['new_label']!='')) {
<div class="main-label new-label"><span>'.$row['new_label'].'</span></div>;
} else {
<div class="main-label new-label"><span></span></div>;
}
<!-- HTTP ERROR 500 -->
<div class="product-image"><img src="'.$row['picture_big'].'" alt="'.$row['name'].'">
You cannot use isset() on the result of an expression, it can only be a variable. In fact, it will result in a fatal error if you try (which is probably why you received a 500 server error).
So you can probably change this line:
if (isset($_row['new_label']!='')) {
To this:
if (isset($_row['new_label'])) {
isset() returns a boolean anyway, so it doesn't make sense to compare it to a string.
Read more: http://php.net/manual/en/function.isset.php
SOLVED MYSELF GUYS
'.$row['new_label'].'';} ?>
thanks anyway

Issue with bootstrap grid and mysql response

I have an issue with bootstrap and creating a 4 column responsive grid from a mysql response.
The problem is that if the second mysql query has a variable number of results, it brakes the grid.
Here is my code (where the first query has 9 results and the second query has a variable number of results):
<?php
$a = "SELECT * FROM $table_users ORDER BY username";
$result = mysql_query($a);
?>
<div class="container">
<div class="row">
<?php while ($row = mysql_fetch_array($result)) {?>
<div class="col-xs-3" style="background-color:aqua;">
<?php echo $row['username'];
$b = "SELECT * FROM $table_presents WHERE bought_for='$row[username]' OR bought_for='' ORDER BY id";
$result_presents = mysql_query($b) or die(mysql_error());
while ($row_presents = mysql_fetch_array($result_presents)) {
?>
<div style="background-color:red;">
Hello world!
</div>
<?php }?>
</div>
<?php }?>
</div>
</div>
which gives me this:
enter image description here
instead of this (obviously with many 'Hello world'):
enter image description here
Any help greatly appreciated!
Bootstrap doesn't claim to do any kind of elegant bin-packing on panels with different sizes. You could do some programming or css work to make all your panels the same size.
If that doesn't work for your application, you're going to need a layout library that does bin-packing so these panels of different sizes align properly.
There are a few such libraries available as jQuery plugins.
In this, $row[username] is wrong as it should be $row['username'].
$b = "SELECT * FROM $table_presents WHERE bought_for='$row[username]' OR bought_for='' ORDER BY id";
BTW, I changed your code bit. Please Try this.
<?php
$a = "SELECT * FROM $table_users ORDER BY username";
$result = mysql_query($a);
?>
<div class="container">
<div class="row">
<?php while ($row = mysql_fetch_array($result))
{
$username=$row['username'];
?>
<div class="col-xs-3" style="background-color:aqua;">
<?php echo $username;
$b = "SELECT * FROM $table_presents WHERE bought_for='$username' OR bought_for='' ORDER BY id";
$result_presents = mysql_query($b) or die(mysql_error());
while ($row_presents = mysql_fetch_array($result_presents)) {
?>
<div style="background-color:red;">
Hello world!
</div>
<?php }?>
</div>
<?php }?>
</div>
</div>
[NOTE: Users can inject your SQL commands. Use prepared statements and parameterized queries. For more info, click Prevent SQL Injections

How to insert echo into div

I'm new here, so do not be angry if I ask something that is already answered.
I connected sql database:
connect.php
<?php
$connect = mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("mobilni", $connect) or die(mysql_error());
?>
displaydata.php
<?php
include "connection.php";
$sql= "SELECT * FROM imena WHERE Okrug='Beogradski'";
$query=mysql_query($sql) or die (mysql_error());
?>
<div class="beyondheader"></div>
<div class="header">
<div id='cssmenu'>
<ul>
<li class='active'><a href='index.html'>Početna</a></li>
<li><a href='proizvodjaci.html'>Proizvodjači</a></li>
<li><a href='oglasi.html'>Oglasi</a></li>
<li><a href='about.html'>O nama</a></li>
</ul>
</div>
</div>
<div class="middle">
<div class="leftmiddle">ss
</div>
<div class="rightmiddle">
<?php
while ($row = mysql_fetch_array($query)){
?>
<div class="divmobilni">
Ime:<div class="mobilniime"><?php echo $row['Ime'];?></div>
Okrug:<?php echo $row['Okrug'];?>
</div>
<?php } ?>
</div>
</div>
And everything is working fine. Now I want to put every result in the other <div> automatically?
How to do that?
Sorry if I wasn't clear enough. I want to sort results to be like this, in one <div> goes: Ime: Okrug: [that is data for one person]
Now, I want to make that all data from my SQL table display on this page, but every Person separately from this <div>.
To be something like this, with data from table: (This is just example drawn in Paint)
I fixed this with adding only break to the end of PHP. Thanks in any case!
Exactly the same as you would with php open and close <?php ?>
Use this inside the divs you would like or just use echo with the div inside

Render data from PHP DB query in HTML

I am a newbie to PHP but trying to learn it to enhance my programming skillset
So far i have the following PHP code in my page to return some data from my Database:
<?php
//code missing to retrieve my data
while($row = mysql_fetch_array($result))
{
echo '$row['Name']';
}
mysql_close($connection);
?>
This is working in that I can see the names from my database displayed on screen. I have seen as well that I can include html in the echo to format the data. However if I have the html code like below in a jQuery accordion outside my PHP code in the page - how can I dynamically place the Names in the specific h3 tags - so the first name in my table is Joe so that comes back in [0] element of array - is there a way I can reference this from my html code outside the php?
<div id="accordion">
<h3>Joe</h3>
<div>
<p>
Some blurb about Joe
</p>
</div>
<h3>Jane</h3>
<div>
<p>
Some blurb about Jane
</p>
</div>
<h3>John</h3>
<div>
<p>
Some Blurb about John.
</p>
</div>
</div>
Try something like this:
<?php while($row = mysql_fetch_array($result)) { ?>
<h3><?php echo $row['name']; ?></h3>
<div>
<p>Some blurb about Joe</p>
</div>
<?php } ?>
I'm assuming 'Some blurb about Joe' would also have to be replaced by a field in the DB, which you can accomplish in the same manner as the name.
#Gert is correct - the original mysql API is deprecated and should not be used anymore. Look into mysqli or PDO, instead.
add your html in while loop like this
<?php
//code missing to retrieve my data
while($row = mysql_fetch_array($result))
{
?>
<h3><?php echo $row['Name']?></h3>
<div>
<p>
Some blurb about <?php echo $row['Name']?>
</p>
</div>
<?php
}
mysql_close($connection);
?>
Like this :
<div id="accordion">
<?php
while($row = mysql_fetch_array($result))
{
<h3><?php echo $row['Name'] ?></h3>
<div>
<p>
Some blurb about Joe
</p>
</div>
} ?>
</div>

JQuery mobile add collapsible content dynamically php

I am building a webapp with jquery mobile where I need to get data from external database and show that data as collapsible content.
<div data-role="collapsible" data-theme="b" data-content-theme="d" data-collapsed="true">
<?php
// display the results returned
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
printf('<h3><b> %s </h3>
<p> %s <p>',$row["OSO"], $row["AIKA"]);
}
?>
How can I make every H3 to appear as a collapsible content title? Now only the first is shown as a title and the rest are in the content section
You should make a new collapsible item for each entry if you want multiple headers...
<?php
// display the results returned
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<div data-role="collapsible" data-theme="b" data-content-theme="d" data-collapsed="true">
<h3><?= $row["OSO"] ?></h3>
<p><?= $row["AIKA"] ?><p>
</div>
<?php } ?>

Categories