Turn this into an if / else statement - php

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) {

Related

Generate HTML code with PHP automatically, after data pull in MySql

I have written a really simple php page that populate a database.
I now want to fetch this data in another php page and that is ok.
What I would like to achive is:
when I add another row into the database, I would like the html to create a new card, and not add the information in the same card.
I am not sure I understand how this can be achived.
Do I have to use php templates like smarty or anybody can point me how could I proceed?
This is how it look when I add second raw:
While what i want to achive should look like
Here is the HTML code I use with the PHP code:
<section class="tm-section-3 tm-section-mb" id="tm-section-3">
<div class="row">
<div class="col-md-6 tm-mb-sm-4 tm-2col-l">
<div class="image">
<img src="img/tm-img-1.jpg" class="img-fluid" />
</div>
<div class="tm-box-3">
<h2>
<?php if (mysqli_num_rows($result) > 0) {
?>
<table>
<?php
include_once '../scripts/connection.php';
$result = mysqli_query($link,"SELECT
domain,subdomain,topic,topictitle,topictext FROM newpage");
$i=0;
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $row["domain"]; ?></td>
</tr>
<tr>
<td><?php echo $row["subdomain"]; ?></td>
</tr>
<tr>
<td><?php echo $row["topic"]; ?></td>
</tr>
<tr>
<td><h4><?php echo $row["topictitle"]; ?></h4></td>
</tr>
<tr>
<td><h5><?php echo $row["topictext"]; ?></h5></td>
</tr>
<?php
$i++;
}
?>
</table>
<?php
}
else{
echo "No result found";
}
?>
</h2>
<p>
</p>
<div class="text-center">
Details
</div>
</div>
</div>
</div>
</section>
This is how i send the code to the db:
<?php
include("connection.php");
$domain = mysqli_real_escape_string($link, $_POST['domain']);
$subdomain = mysqli_real_escape_string($link, $_POST['subdomain']);
$topic = mysqli_real_escape_string($link, $_POST['topic']);
$topictitle = mysqli_real_escape_string($link, $_POST['topictitle']);
$topictext = mysqli_real_escape_string($link, $_POST['topictext']);
$sql = "INSERT INTO newpage (domain,subdomain,topic,topictitle,topictext) VALUES ('$domain','$subdomain','$topic','$topictitle','$topictext')";
$result = mysqli_query($link, $sql);
// if query fails stop script and echo error
if( $result === false)
{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
exit;
}
$sql = "INSERT INTO menu (item) VALUES ('$domain')";
$result = mysqli_query($link, $sql);
// if query fails stop script and echo error
if( $result === false)
{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
exit;
}
header("location:../scripts/add-new-page-script-end.php");
exit;
echo "You'll never see this";
?>
Here the code that works even the style is bad. But logically is correct:
<div class="col-md-6 tm-mb-sm-4 tm-2col-l">
<?php
include_once '../scripts/connection.php'; $result = mysqli_query($link,"SELECT domain,subdomain,topic,topictitle,topictext FROM newpage"); foreach($result as $row){
?>
<div class="image">
<img src="img/tm-img-1.jpg" class="img-fluid" />
</div>
<div class="tm-box-3">
<h1><?php echo $row['domain']; ?></h1>
<h2><?php echo $row['subdomain']; ?></h2>
<h3><span><?php echo $row['topic']; ?></span></h3>
<h4> <span><?php echo $row['topictitle']; ?></span></h4>
<p><?php echo $row['topictext']; ?></p>
<div class="text-center">
Details
</div>
</div>
<?php
}
?>
</div>
It currently looks like you have something like this:
<div class="card">
<img src="..." />
<?php
foreach($result as $row){
?>
<h1><?php echo $row['domain-name']; ?></h1>
<h2><?php echo $row['sub-domain-name']; ?></h2>
<span><?php echo $row['topic-text-title']; ?></span>
<p><?php echo $row['text-of-topic']; ?></p>
<?php
}
?>
<button>Details</button>
</div>
If you instead put the foreach loop outside of the card div then it will make a new card for each result, something like this:
<?php
foreach($result as $row){
?>
<div class="card">
<img src="..." />
<h1><?php echo $row['domain-name']; ?></h1>
<h2><?php echo $row['sub-domain-name']; ?></h2>
<span><?php echo $row['topic-text-title']; ?></span>
<p><?php echo $row['text-of-topic']; ?></p>
<button>Details</button>
</div>
<?php
}
?>
Assuming that your are not using any framework.
In raw php context you could do something like this:
<div>
<?php foreach($arr as $item): ?>
<div>
<img src="...">
<h1><?php echo $item->domain_name; ?></h1>
<h2><?php echo $item->subdomain_name; ?></h2>
<h3><?php echo $item->topic; ?></h3>
<h4><?php echo $item->topic_text_title; ?></h4>
<h5><?php echo $item->text_pf_topic; ?></h5>
</div>
<?php endforeach; ?>
</div>

PHP Bootstrap row loop every 3 posts

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; ?>">

Showing fields from a query in php

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>

While loop inside a while loop from PDO query

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

each query a different background-image

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
}

Categories