Consider a situation where you have a mysql table FLAGS with a random amount of rows
ID Language Flag1 Flag2 Flag3 Flag4
1 dutch flanders_flag.png dutch_flag.png
2 french wallonia_flag.png french_flag.png morocco_flag.png
3 english england_flag.png ireland_flag.png america_flag.png scotland_flag.png
if i want to query these rows and put them into my html i use the while loop because i never know how much rows this table FLAGS has.
<?php
$flagquery = $db->prepare ("SELECT * FROM flags");
$flagquery->execute();
while ($flagrow = $flagquery->fetch(PDO::FETCH_ASSOC)) {
?>
<div class="col-md-1 col-sm-2 col-xs-6">
<p><?php echo $flagrow['language']; ?></p>
<p><?php echo $flagrow['flag1']; ?></p>
<p><?php echo $flagrow['flag2']; ?></p>
<p><?php echo $flagrow['flag3']; ?></p>
<p><?php echo $flagrow['flag4']; ?></p>
</div>
<?php
}
?>
But as you can see in the FLAGS table you don't always have 4 flags a language so i would think you have to do a while loop inside this previous while loop to echo only the flags that are present in the FLAGS table instead of just echo all the flags even if they are empty.
Are my thoughts right? Or what would be the best way to handle my situation?
No you don't need a second loop , you can use !empty() . it will return false if the variable is an empty string, false, array(), NULL, “0?, 0, and an unset variable . it will be useful in your case and in case you allow FLAGS to have NULL value.
<?php
$flagquery = $db->prepare ("SELECT * FROM flags");
$flagquery->execute();
while ($flagrow = $flagquery->fetch(PDO::FETCH_ASSOC)) {
?>
<div class="col-md-1 col-sm-2 col-xs-6">
<p><?php echo $flagrow['language']; ?></p>
<?php if(!empty($flagrow['flag1'])) echo '<p>'.$flagrow['flag1'].'</p>'; ?>
<?php if(!empty($flagrow['flag2'])) echo '<p>'.$flagrow['flag2'].'</p>'; ?>
<?php if(!empty($flagrow['flag3'])) echo '<p>'.$flagrow['flag3'].'</p>'; ?>
<?php if(!empty($flagrow['flag4'])) echo '<p>'.$flagrow['flag4'].'</p>'; ?>
</div>
<?php
}
?>
<?php
$flagquery = $db->prepare ("SELECT * FROM flags");
$flagquery->execute();
while ($flagrow = $flagquery->fetch(PDO::FETCH_ASSOC)) {
?>
<div class="col-md-1 col-sm-2 col-xs-6">
<p><?php echo $flagrow['language']; ?></p>
<p><?php if($flagrow['flag1']!='') echo $flagrow['flag1']; ?></p>
<p><?php if($flagrow['flag2']!='') echo $flagrow['flag2']; ?></p>
<p><?php if($flagrow['flag3']!='') echo $flagrow['flag3']; ?></p>
<p><?php if($flagrow['flag4']!='') echo $flagrow['flag4']; ?></p>
</div>
<?php
}
?>
check if variable is empty then it comes in if else not
Related
When an item is chosen on my site, it opens a details page. This is the top of the details page above the html tags:
<?php require_once('dbconnection.php');
mysqli_select_db($conn, $dbname);
$recordID = $_GET['recordID'];
$query_Master_details = "
SELECT *
FROM Master_List
WHERE Master_List.Master_Id = $recordID
";
$Master_details = mysqli_query($conn, $query_Master_details) or die(mysqli_error());
$row_Master_details = mysqli_fetch_assoc($Master_details);
$totalRows_Master_details = mysqli_num_rows($Master_details);
?>
This is the code that makes the body of the page:
<div class="container2">
<div class="category"><h2><?php echo $row_Master_details['Name']; ?></h2></div>
<p><?php echo $row_Master_details['Name']; ?></p>
<p><img src="img/<?php echo $row_Master_details['Img']; ?>" /></p>
<p><?php echo $row_Master_details['Code']; ?></p>
<p><?php echo $row_Master_details['Length']; ?> Characters</p>
<?php
mysqli_free_result($Master_details);
?>
<!-- end .container2 --></div>
What I would like to do is create an if/else statement that will look at the Style_ID of the selected item and determine if the number is > 3. If it is, I want it to choose an item that has a Style_Id of 1, 2, or 3 and the same Length as the item chosen and return a random row in the layout above, skip a few lines and then display the information for the selected item in the layout above. Else if it is < or = 3, then I need it to just display as above.
I have tried using:
<?php
If (Style_ID > 3) {
echo 'Test';
}Else {
<div class="category"><h2><?php echo $row_Master_details['Name']; ?></h2></div>
<p><?php echo $row_Master_details['Name']; ?></p>
<p><img src="img/<?php echo $row_Master_details['Img']; ?>" /></p>
<p><?php echo $row_Master_details['Code']; ?></p>
<p><?php echo $row_Master_details['Length']; ?> Characters</p>
}
?>
<?php
mysqli_free_result($Master_details);
?>
But it doesn't work and has syntax errors. How can I create this if/else statement?
Note: I would appreciate being able to get one setup for all of it, but if not just fixing this part would be a big help right now.
Thanks to #Brad for his responses, I finally got this one figured out. I ended up changing some of my field names and finally figured out where to close the php tags to make this work. Here is what I ended up with:
<div class="container2">
<div class="category"><h2><?php echo $row_master_details['name']; ?></h2></div>
<?php
if ($row_master_details['type_id'] > 3) {
echo "Test";
}else { ?>
<p><?php echo $row_master_details['name']; ?></p>
<p><img src="img/<?php echo $row_master_details['img']; ?>" /></p>
<p><?php echo $row_master_details['item_code']; ?></p>
<p><?php echo $row_master_details['length']; ?> Characters</p>
<?php
mysqli_free_result($master_details);
?>
<?php } ?>
<!-- end .container2 --></div>
For starters, PHP is case-sensitive.
If (...) {
...
}Else {
You'll want to use lower-case if and else.
Next, if Style_ID is an attribute of the record, you'll need to access it like you did the others.
if ($row_Master_details['Style_ID'] > 3) {
Trying to display the custom posts on my archive page within a bootstrap row containing 3 columns then starting a new row, got the code but new to PHP and dont know where to put the content.
<?php
//Columns must be a factor of 12 (1,2,3,4,6,12)
$numOfCols = 3;
$rowCount = 0;
$bootstrapColWidth = 12 / $numOfCols;
?>
<div class="row">
<?php
foreach ($rows as $row){
?>
<div class="col-md-4"<?php echo $bootstrapColWidth; ?>">
<div class="thumbnail">
<img src="user_file/<?php echo $row->foto; ?>">
</div>
</div>
<?php
$rowCount++;
if($rowCount % $numOfCols == 0) echo '</div><div class="row">';
}
?>
</div>
<div class="embed-container">
<?php the_field('podcast_embed_link'); ?>
</div>
<h3><?php the_title(); ?></h3>
<p><b><?php echo $date->format( $format_out );?></b></p>
<p><?php the_field('description'); ?></p>
<?php if( get_field('thumbnail') ): ?>
<img src="<?php the_field('thumbnail'); ?>" />
<?php endif; ?>
<?php endwhile; // end of the loop. ?>
</div>
</div>
</div><!-- #content -->
Here is the code for the page archive.podcasts.php, where would i add the custom fields within the row loop?
First of all, you don't need to close and open row tag each 3 items. If you leave the code like this:
<div class="row">
<?php
foreach ($rows as $row){
?>
<div class="col-md-<?php echo $bootstrapColWidth; ?>">
<div class="thumbnail">
<img src="user_file/<?php echo $row->foto; ?>">
</div>
</div>
<?php
}
?>
</div>
you will get the same effect, but without the separation that a row tag involves. Notice that the line involving "col-md-4" has already changes in order to not create wrong col size usage.
In this part of code:
<div class="col-md-4"<?php echo $bootstrapColWidth; ?>">
You must get wrong bootstrap class like col-md-41, col-md-412.
Correct you code by this way:
<div class="col-md-<?php echo $bootstrapColWidth; ?>">
I'm new in PHP and i'm having a trouble in showing the retrived data from a query. There area four area_name and many type_name, type_desc and type_price under that area_name
I wanted to display those data in a format like this:
area_name1
type_name1_1
type_desc1_1
type_price1_1
type_name1_2
type_desc1_2
type_price1_2
then
area_name2
type_name2_1
type_desc2_1
type_price2_1
type_name2_2
type_desc2_2
type_price2_2
But in my code, it is displayed like this:
area_name1
type_name1_1
type_desc1_1
type_price1_1
area_name1
type_name1_2
type_desc1_2
type_price1_2
then
area_name2
type_name2_1
type_desc2_1
type_price2_1
area_name2
type_name2_2
type_desc2_2
type_price2_2
Can anyone help me on how to show the area_name once then show the type_names under that specific area_name? any help/assistance will be greatly appreciated.
<?php
include 'config.php';
$query = "select a.area_name,t.type_no,t.type_name,t.type_desc,t.type_price,t.area_no, a.area_no from area as a INNER JOIN type as t where a.area_no=t.area_no";
$stmt= dbConnect()->prepare($query);
$stmt -> execute();
$text_num = $stmt -> rowCount();
?>
<div id="prices_content">
<div>
<?php if ($text_num>0) {?>
<?php while ($row=$stmt->fetch(PDO::FETCH_ASSOC)) {?>
<div id="content">
<br><br><br><center>
<b><h1><?php echo $row['area_name']; ?></h1></b><br>
<p><?php echo $row['type_name']; ?></p><br>
<p><?php echo $row['type_desc']; ?></p><br>
<p><?php echo $row['type_price']; ?></p></center><br>
</div>
<?php }?>
<?php }?>
</div>
</div>
In your while loop you're echoing $row['area_name'] every time. You need to declare a variable outside the loop to hold the area name and check it in each loop to see if it changed.
Like this:
$area_name = '';
<?php while ($row=$stmt->fetch(PDO::FETCH_ASSOC)) {?>
<div id="content">
<br><br><br><center>
<?php if($area_name != $row['area_name']){ ?>
<?php $area_name = $row['area_name']; ?>
<b><h1><?php echo $row['area_name']; ?></h1></b><br>
<?php } ?>
<p><?php echo $row['type_name']; ?></p><br>
<p><?php echo $row['type_desc']; ?></p><br>
<p><?php echo $row['type_price']; ?></p></center><br>
</div>
<?php } ?>
Try this Code,
<?php
include 'config.php';
$query = "select a.area_name,t.type_no,t.type_name,t.type_desc,t.type_price,t.area_no, a.area_no from area as a INNER JOIN type as t where a.area_no=t.area_no";
$stmt= dbConnect()->prepare($query);
$stmt -> execute();
$text_num = $stmt -> rowCount();
?>
<div id="prices_content">
<div>
<?php if ($text_num>0) {?>
<?php while ($row=$stmt->fetch(PDO::FETCH_ASSOC)) {?>
<div id="content">
<br><br><br><center>
<?if($area_name !== $row['area_name']){?>
<b><h1><?php echo $row['area_name']; ?></h1></b><br>
<?php }?>
<p><?php echo $row['type_name']; ?></p><br>
<p><?php echo $row['type_desc']; ?></p><br>
<p><?php echo $row['type_price']; ?></p></center><br>
</div>
<?php $area_name = $row['area_name'];}?>
<?php }?>
</div>
</div>
You are adding the area_name into the loop so it will be displayed
you should try this
<div id="prices_content">
<div>
<?php if ($text_num>0) {?>
<b><h1><?php echo $row['area_name']; ?></h1></b><br><!-- this outside of the loop-->
<?php while ($row=$stmt->fetch(PDO::FETCH_ASSOC)) {?>
<div id="content">
<br><br><br><center>
<p><?php echo $row['type_name']; ?></p><br>
<p><?php echo $row['type_desc']; ?></p><br>
<p><?php echo $row['type_price']; ?></p></center><br>
</div>
<?php }?>
<?php }?>
</div>
I am currently using the following code to get all the rows of my mysql column:
$result = mysqli_query($dbc,"SELECT * FROM offers");
$records = mysqli_num_rows($result);
while ($row = mysqli_fetch_row($result)) {
?>
<div class="row">
<div class="col-md-4 portfolio item">
<h3><? echo $row[1] ?></h3>
<p><? echo $row[2] ?></p>
<p><? echo $row[3] ?></p>
<p><? echo $row[4] ?></p>
</div>
</div>
<?
}
It displays all the rows info as html but the problem is: In 4th column of every row I am storing a unique url that I have to print. Simply $row[4] will print the url stored in the first row.
TABLE STRUCTURE
OfferID OfferName OfferDesc OfferLink OfferCredits OfferInstructions
What should I do? :(
You can use mysqli_fetch_array function to get an array of the query result and then use the row and column indexes to get value from the specific row and column
$result = mysqli_query($dbc,"SELECT * FROM offers");
$records = mysqli_num_rows($result);
$arrayofrows = array();
while ($row = mysqli_fetch_array($result))
{
$arrayofrows = $row;
?>
<div class="row">
<div class="col-md-4 portfolio item">
<h3><? echo $arrayofrows[1] ?></h3>
<p><? echo $arrayofrows[2] ?></p>
<p><? echo $arrayofrows[3] ?></p>
<p><? echo $arrayofrows[1][4] ?></p> <?php } ?>
</div>
</div>
From the code above, $arrayofrows[1] means the second row, therefore $arrayofrows[1][4] means the second row and the fourth column.
Alternatively, you can try this:
$query = "SELECT * FROM offers";
$results = mysqli_query($cpnnection, $query) or die(mysqli_error($connection));
if (mysqli_num_rows($results) > 0) {
while ($rows = mysqli_fetch_assoc($results)) {
<div class="row">
<div class="col-md-4 portfolio item">
<h3><? echo $rows["OfferID"] ?></h3>
<p><? echo $rows["OfferName"] ?></p>
<p><? echo $rows["OfferDesc"] ?></p>
<p><? echo $rows["OfferLink"] ?></p>
<p><? echo $rows["OfferCredits"] ?></p>
<p><? echo $rows["OfferInstructions"] ?></p>
</div>
</div>
I build this in php:
<?php
$bg = array('block2.png', 'block3.png', 'block4.png', 'block5.png', 'block7.png' );
$i = rand(0, count($bg)-1);
$selectedBg = "$bg[$i]";
?>
<style type="text/css">
<!--
.block_small{
background-image: url(/tableaux/customer/images/<?php echo $selectedBg; ?>);
}
-->
</style>
<?php
$db = new DB();
$db->query("SELECT * FROM vestigingen ");
while($item = $db->next_record()){
?>
<div class="block_small">
<img src="<?php echo $item['afbeelding']; ?>" />
<h1><?php echo $item['plaats']; ?></h1>
<div class="content_blocks">
<p><?php echo $item['adres']; ?></p>
<p><?php echo $item['postcode']; ?></p>
<p><?php echo $item['telefoon']; ?></p>
<p><?php echo $item['mail']; ?></p>
</div>
<div class="arrow"><?php echo $item['link']; ?></div>
</div>
<?
}
?>
Content loads from a SQL database, I want every block to have a different background-image, now they are all the same.
I first tried to put the style in the while loop but that didn't work.
Idea's ?
If you want all the div containers to have a different background you cannot append the same class to all of them. The image in the class is chosen randomly, but afterwards the same image-background is given to all the div elements with the same class.
With only php you got two possibilities to set a different background for all of the images:
first solution:
write a loop and set the css for ids #small_block1-#small_blockx.
second solution:
set inline css for all the elements within the style tag in a loop. I will show this solution below.
<?php
$bg = array('block2.png', 'block3.png', 'block4.png', 'block5.png', 'block7.png' );
$db = new DB();
$db->query("SELECT * FROM vestigingen ");
$counter = 0;
while($item = $db->next_record()){
?>
<div class="block_small" style="background-image: url(/tableaux/customer/images/<?php echo $bg[$counter]; ?>);" >
<img src="<?php echo $item['afbeelding']; ?>" />
<h1><?php echo $item['plaats']; ?></h1>
<div class="content_blocks">
<p><?php echo $item['adres']; ?></p>
<p><?php echo $item['postcode']; ?></p>
<p><?php echo $item['telefoon']; ?></p>
<p><?php echo $item['mail']; ?></p>
</div>
<div class="arrow"><?php echo $item['link']; ?></div>
</div>
<?
$counter++;
}
?>
You are do it in wrong way, you are always update the style. Instead of this, add an inline style.
<?php
$bg = array('block2.png', 'block3.png', 'block4.png', 'block5.png', 'block7.png');
$db = new DB();
$db->query("SELECT * FROM vestigingen ");
while ($item = $db->next_record()) {
$selectedBg = $bg[array_rand($bg)];
?>
<div class="block_small" style="background-image: url('/tableaux/customer/images/<?php echo $selectedBg; ?>)';">
<img src="<?php echo $item['afbeelding']; ?>" />
<h1><?php echo $item['plaats']; ?></h1>
<div class="content_blocks">
<p><?php echo $item['adres']; ?></p>
<p><?php echo $item['postcode']; ?></p>
<p><?php echo $item['telefoon']; ?></p>
<p><?php echo $item['mail']; ?></p>
</div>
<div class="arrow"><?php echo $item['link']; ?></div>
</div>
<?php
}