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>
Related
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 if(isset($_POST["btn-add"])):?>
<?php $element = 0; ?>
<?php while($element < 10): ?>
<?php $statement = $pdo->query("SELECT * FROM dorayaki ORDER BY stock DESC LIMIT $element ,1"); ?>
<?php if($row = $statement->fetch()): ?>
<div name ="card" class = "card">
<?php $doraId = $row["id"] ; ?>
<p><?php print_r("Name: "."<a name='doraprice' href='detail.php?id=$doraId'>".$row["name"]."</a>"); ?></p>
<p><?php print_r("Price: "."<a name='doraprice' href='detail.php?id=$doraId'>".$row["price"]."</a>"); ?></p>
<p><?php print_r("Description: "."<a name='doraprice' href='detail.php?id=$doraId'>".$row["desc"]."</a>"); ?></p>
</div>
<?php $element++; ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
<form method="post">
<input type="submit" name="btn-add" value="Add element">
</form>
Every time "Add element" is clicked, I want it to append one 'card' to the html page and if the button is clicked again, it will add one more 'card' until it reaches 10 cards. Instead of appending it one by one, when I clicked the "Add Button", it appends all the cards to the html page in once. How to solve this?
You must save $element in a cookie or a session variable:
<?php session_start(); ?>
<?php if(isset($_POST["btn-add"])):?>
<?php $_SESSION["element"] = isset($_SESSION["element"])?0:; ?>
<?php for($i=0;$_SESSION["element"]<$i;$_SESSION["element"]): ?>
<?php $statement = $pdo->query("SELECT * FROM dorayaki ORDER BY stock DESC LIMIT $element ,1"); ?>
<?php if($row = $statement->fetch()): ?>
<div name ="card" class = "card">
<?php $doraId = $row["id"] ; ?>
<p><?php print_r("Name: "."<a name='doraprice' href='detail.php?id=$doraId'>".$row["name"]."</a>"); ?></p>
<p><?php print_r("Price: "."<a name='doraprice' href='detail.php?id=$doraId'>".$row["price"]."</a>"); ?></p>
<p><?php print_r("Description: "."<a name='doraprice' href='detail.php?id=$doraId'>".$row["desc"]."</a>"); ?></p>
</div>
<?php $element++; ?>
<?php endif; ?>
<?php endfor; ?>
<?php endif; ?>
<form method="post">
<input type="submit" name="btn-add" value="Add element">
</form>
I am trying with php 7.0 as everyone recommend me here to start with it as 5.6 has been depreveted . But after writing its code i am unable to fetch the values from database. what wrong i am doing here. thank you
//This is my aboutus page//
<div class="col-lg-10">
<?php include('config.php');
$query = mysqli_query('select * from about ');
while($row = mysqli_fetch_array($query)){
?>
<h5>
<?php echo $row['about_desc'] ;?>
<br>
<br>
<?php echo $row['about_desc1']; ?>
<br>
<br>
<?php echo $row['about_desc2'] ;?>
<br>
<br>
<?php echo $row['about_desc3'] ;?>
<br>
<br>
<?php echo $row['about_desc4']; ?>
</h5>
<hr class="featurrate-divider">
<?php
}
?>
</div>
</div>
<div id="tab-1">
<?php include('config.php ');
$query = mysqli_query('select * from news LIMIT 0,2');
while($row = mysqli_fetch_array($query)){
?>
<div class="news">
<h4><u><?php echo $row['news_name']; ?></u></h4>
<h5><font color="black"><?php echo $row['dat']; ?></font></h5>
<p><?php echo $row['news_desc']; ?></p>
se mer >>
</div>
<hr class="featurrate-divider">
<?php
}
?>
</div>
//This is my config.php file//
<?php
$connection = mysqli_connect('localhost','root','','dandelion') or die(mysqli_error($connection));
?>
Looking for good suggestion as a learner. thank you
Your About us page should be like this:
<body>
<div class="col-lg-10">
<?php include('config.php');
$query = mysqli_query($connection, 'select * from about ');
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
?>
<h5>
<?php echo $row['about_desc'] ;?>
<br>
<br>
<?php echo $row['about_desc1']; ?>
<br>
<br>
<?php echo $row['about_desc2'] ;?>
<br>
<br>
<?php echo $row['about_desc3'] ;?>
<br>
<br>
<?php echo $row['about_desc4']; ?>
</h5>
<hr class="featurrate-divider">
<?php
}
?>
</div>
</div>
<div id="tab-1">
<?php include('config.php ');
$query = mysqli_query($connection, 'select * from news LIMIT 0,2');
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
?>
<div class="news">
<h4><u><?php echo $row['news_name']; ?></u></h4>
<h5><font color="black"><?php echo $row['dat']; ?></font></h5>
<p><?php echo $row['news_desc']; ?></p>
se mer >>
</div>
<hr class="featurrate-divider">
<?php
}
?>
</div>
</body>
if your database connection is sucessfully work and problem with fetching data than use number.it may be useful to fetch the data.
<?php echo $row[o]; ?>
<?php echo $row[1]; ?>
instead of
<?php echo $row['about_desc1']; ?>
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
}
I have created a yii based project. I have a view where we show data from a database. I want to print that page. I searched and found the printout extension for printing and I copied the 'print' directory to /protected/extensions, but we can not understand how we could use it on my view page to print it.
My view page:
<div class="row">
<div class="row-column1">
<?php echo CHtml::label('Reg. Number. :','student_enroll_no'); ?>
<?php echo $info->student_enroll_no;?>
</div>
<div class="row-column2">
<?php echo CHtml::label('Adm. Date :','student_adm_date'); ?>
<?php if($info->student_adm_date != NULL)
echo date('d-m-Y',strtotime($info->student_adm_date));?>
</div>
</div>
<div class="row">
<div class="row-column1">
<?php echo CHtml::label('Student Name :','student_first_name'); ?>
<?php echo $info->student_first_name;?>
</div>
<div class="row-column2">
<?php echo CHtml::label('Enroll No. :','student_mobile_no'); ?>
<?php echo $info->student_mobile_no;?>
</div>
</div>
<div class="row">
<div class="row-column1">
<?php echo CHtml::label('Fathers Name :','student_last_name'); ?>
<?php echo $info->student_last_name;?>
<!--<?php echo CHtml::label('Gender :','student_gender'); ?>
<?php echo $info->student_gender;?>-->
</div>
<div class="row-column3">
<?php echo CHtml::label('Mothers Name :','student_mother_name'); ?>
<?php echo $info->student_mother_name;?>
<!-- <?php echo CHtml::label('Date of Birth :','student_dob'); ?>
<?php if($info->student_dob != NULL)
echo date('d-m-Y',strtotime($info->student_dob));?>-->
</div>
</div>
<div class="row">
<div class="row-column1">
<?php echo CHtml::label('Gender :','student_gender'); ?>
<?php echo $info->student_gender;?>
<!--<?php echo CHtml::label('Course :','student_transaction_course_id'); ?>
<?php
echo !empty($model->student_transaction_course_id) ? $model->relCourse->course_name : 'N/A';
?>-->
</div>
<div class="row-column2">
<?php echo CHtml::label('Date of Birth :','student_dob'); ?>
<?php if($info->student_dob != NULL)
echo date('d-m-Y',strtotime($info->student_dob));?>
<!--<?php echo CHtml::label('Year :','student_academic_term_period_tran_id'); ?>
<?php echo $model->Rel_student_academic_terms_period_name->academic_term_period;?>-->
</div>
</div>
<div class="row">
<div class="row-column1">
<?php echo CHtml::label('Category :','title'); ?>
<?php echo $info->title;?>
</div>
<div class="row-column2">
<?php echo CHtml::label('Email ID :','student_email_id_1'); ?> <?php echo $info->student_email_id_1; ?>
</div>
</div>
<div class="row">
<div class="row-left">
<?php echo CHtml::label('Course :','student_transaction_course_id'); ?>
<?php
echo !empty($model->student_transaction_course_id) ? $model->relCourse->course_name : 'N/A';
?>
<!-- <?php echo CHtml::label('Blood Group :','student_living_status'); ?>
<?php echo $info->student_living_status; ?>-->
</div>
<div class="row-column3">
<!-- <?php echo CHtml::label('Nationality :','student_transaction_nationality_id'); ?>
<?php if($model->student_transaction_nationality_id!=null)
echo $model->Rel_Nationality->nationality_name;
?>-->
<?php echo CHtml::label('Year :','student_academic_term_period_tran_id'); ?>
<?php echo $model->Rel_student_academic_terms_period_name->academic_term_period;?>
</div>
</div>
<div class="row">
<div class="row-left">
<?php echo CHtml::label('Subjects :','languages_known1'); ?>
<?php
$knwLang = "";
if($lang->languages_known1)
$knwLang = $lang->Rel_Langs1->languages_name;
if($lang->languages_known2)
$knwLang .= ", ".$lang->Rel_Langs2->languages_name;
if($lang->languages_known3)
$knwLang .= ", ".$lang->Rel_Langs3->languages_name;
echo $knwLang;
?>
</div>
</div>
<div class="row last">
<?php echo ('_____________________Note -Click on edit and update DoB , Subjects and upload photo ,Enroll no. .'); ?>
</div>
The documentation for this extension says it is tuned for the dataGrid widget. However it seems you are not using the dataGrid widget so what you can do is override the default printedElement property.
For example:
Wrap all the elements you wish to print in a div and give it an id of #printme and add the following to your view.
<?php
$this->widget('application.extensions.print.printWidget', array(
'cssFile' => 'print.css',
'printedElement'=>'#printme',
)
)
);
?>
NOTE: Don't forget to add/edit the print.css file.
Read the documentation for more options:
http://www.yiiframework.com/extension/printout