changing HTML content in PHP - php

I am newer to PHP and I have a problem changing the resultDiv contents
if(mysql_num_rows($res)==0)
{
echo "<script>document.getElementById('resultDiv').innerHTML='No such flight';</script>";
// echo "<script>alert('No Such filght');</script>";
echo "No such flight";
}
else{
echo "<table border='1'><th>flight#</th><th>Dep. City</th><th>Arrival City</th><th>Dep. Date</th><th>Arrival Date</th><th>Total Seats</th>";
while($row=mysql_fetch_array($res))
echo "<tr><td>".$row['id']."</td><td>".$row['depCity']."</td><td>".$row['arrivCity']."</td><td>".$row['depDate'].
"</td><td>".$row['arrivDate']."</td><td>".$row['totalSeats']."</td></tr>";
echo "</table>";
}
</div>
<div id="resultDiv">
Result Div
</div>

Please avoid to use mysql_* function as it is deprecated in latest version. You can use mysqli or PDO.
You can display message like this :-
<?php
$message = (mysql_num_rows($res) == 0) ? 'No Such Flight' : '';
?>
<div id="resultDiv">
<?php echo $message;?>
</div>

You can do it like so:
<div id="resultDiv">
<?php
if (mysql_num_rows($res) == 0) {
echo "No such flight";
} else {
echo "Flight found";
}
?>
</div>

you have misspelled "resultDiv" in get element by id, also define the div before the if condition.
<div id="resultDiv">
Result Div
</div>
if(mysql_num_rows($res)==0)
{
echo "<script>document.getElementById('resultDiv').innerHTML='No such flight';</script>";
echo "No such flight";
}

Related

How to add a else command in php?

I have an attribute called "Merchant_Name".
The following code will place the "Merchant_Name" Just below the product name in product page.
Now I need to add an else command to this code like this:
If Merchant_Name is there then it should be "By Merchant_Name" else "XXXXX".
Hope you get me...
<div class="Merchant_Name">
<?php
$merchant_name = $_product->getAttributeText('merchant_name');
if ($merchant_name){?>By <?php echo $_product->getResource()->getAttribute('merchant_name')->getFrontend()->getValue($_product); } ?>
</div>
Thanks in Advance...
You mean, with an if/else?
<div class="Merchant_Name">
<?php
$merchant_name = $_product->getAttributeText('merchant_name');
if ($merchant_name) {
echo "By " . $_product->getResource()->getAttribute('merchant_name')->getFrontend()->getValue($_product);
} else {
echo "By XXXXX";
}
?>
Or maybe this would be cleaner:
<?php
if($_product->getAttributeText('merchant_name')) {
$name = $_product->getResource()->getAttribute('merchant_name')->getFrontend()->getValue($_product);
} else {
$name = "XXXXX";
}
?>
<div class="Merchant_Name">By <?php echo $name ?>
Once you start the php tag try to finish your task. Don't switch in and out again and again.
<div class="Merchant_Name">
By
<?php
if ($_product->getAttributeText('merchant_name')){
echo $_product->getResource()->getAttribute('merchant_name')->getFrontend()->getValue($_product);
} else {
echo 'XXXXX';
}
?>
</div>
It is pretty simple and it would be simpler if you didn't switch in and out of PHP so often:
<div class="Merchant_Name">
By
<?php
$merchant_name = $_product->getAttributeText('merchant_name');
if ($merchant_name){
echo $_product->getResource()->getAttribute('merchant_name')->getFrontend()->getValue($_product);
} else {
echo 'XXXXX';
}
?>
</div>

how to check in database for records

i am using a simple database for a guestbook. I just can't figure out how to check if there has nobody written in the guestbook yet. Because in that case, there should be an echo: "Be the first to write in the guestbook". Otherwise, the rows should be echoed.
How can i do that?
Piece of the code:
if (mysqli_connect_errno($con))
{
echo "Connectie Database mislukt: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT name,message,email,datetime FROM guestbook");
while($row = mysqli_fetch_array($result))
{ ?>
<div class="velden"> <!-- voor styling van alle echo's; zie CSS -->
<div class="header">
<div class="naam"><?php echo $row['name']; ?></div> <!-- echo naam-->
<div class="email"><?php echo $row['email']; ?></div> <!-- echo email-->
<div class="tijd"><?php echo $row['datetime']; ?></div> <!-- echo datum en tijd-->
</div>
<div class="bericht"><?php echo $row['message']; ?></div> <!-- echo bericht-->
</div>
<?php } ?>
So there should be something like:
If(nobody has written) {
echo 'Be the first to write in the database";
} else {
//do the echo's
}
I believe mysqli_num_rows (http://php.net/manual/en/mysqli-result.num-rows.php) is what you're after.
if(mysqli_num_rows($result) == 0) {
echo 'Be the first to sign my guestbook!';
} else {
while($row = mysqli_fetch_array($result))
{ ?>
...
<?php } ?>
}
Here's an example from w3 schools: http://www.w3schools.com/php/func_mysqli_num_rows.asp
You can get the number of rows after you execute the query and then if the count is 0 echo "be the first..." else do you while that displays the guestbook... http://www.w3schools.com/php/func_mysqli_num_rows.asp
you should query
SELECT count(*) AS num FROM guestbook
if database is empty result will be 0, otherwise total number or rows in table

Fetch mysql data using while condition in div

Before i was displaying zones records static although i have records in database table
HTML
<div class="pub_fot_sec_menu pub_fot_list_fst">
<h2>Kosi</h2>
<h2>Mechi</h2>
<h2>Sagarmatha</h2>
</div>
<div class="pub_fot_sec_menu pub_fot_list_sec">
<h2>Bagmati</h2>
<h2>Janakpur</h2>
<h2>Narayani</h2>
</div>
<div class="pub_fot_sec_menu pub_fot_list_thrd">
<h2>Dhawalagiri</h2>
<h2>Gandaki</h2>
<h2>Lumbini</h2>
</div>
<div class="pub_fot_sec_menu pub_fot_list_frth">
<h2>Bheri</h2>
<h2>Karnali</h2>
<h2>Rapti</h2>
</div>
Now i want to fetch zone records using while or whatever method that work for me!
<?php
$sql="select * from tb_zone";
$res =mysql_query($sql);
while($data =mysql_fetch_array($res))
{
// want do display zone record in the above html output format
}
?>
Any help would be appreciated!
Try This
<?php
$class = array ('pub_fot_sec_menu pub_fot_list_fst','pub_fot_sec_menu pub_fot_list_sec','pub_fot_sec_menu pub_fot_list_thrd','pub_fot_sec_menu pub_fot_list_frth');
$sql="select * from tb_zone";
$res =mysql_query($sql);
$j=0;
$i=0;
while($data =mysql_fetch_array($res))
{
if($i==0)
{ echo '<div class="'.$class[$j].'">'; }
echo '<h2>'.$data["zone"].'</h2>';
if($i%2==0 && i > 0)
{ echo '</div>'; $j++;$i=0; }
else{ $i++; }
}
?>
just use this type
while($data =mysql_fetch_array($res))
{
echo
'
<div class="pub_fot_sec_menu pub_fot_list_fst">
<h2>'.$data['zone'].'</h2>
';
}

If statement to not show div / h2

This for each / if statement displays changes, and then right below it it displays the changes made.
I am trying to write an if statement that tells it not to show the h2 and the #change_box if there are no changes.
Help would be greatly appreciated.
<h2 class="changes"> Changes: </h2>
<div id="change_box">
<? foreach ($audit['Events'] as $event):?>
<?if ( $event['Type'] != 'Comment'):?>
<span class="field">
<?= $event['Field']?>
</span>:
<?= $event['Value'] ?>
<?=($event['Previous'])?>
<?endif?>
<?endforeach?>
</div>
<?php
if ($changes) { // You'll have to set this variable
//Echo all of your HTML
else {
//Echo the stuff you'd rather show if they didn't change anything
}
?>
To give you an idea of how I'd write your code
<?php
if ($changes) {
echo '<h2 class="changes">Changes:</h2>';
echo '<div id="change_box">';
foreach ($audit['Events'] as $event) {
if ($event['Type'] != 'Comment') {
echo $event['Field'] . </span> . $event['Value'] . $event['Previous'];
}
}
echo "</div>"
}
?>
You could wrap your code with an if like
if(count($audit['Events'])>0)
{
//Your code
}
Wny do you open and close php tags every time? Would be better to make everything in PHP and echo whenever you want to print HTML code.
<?php
echo "<h2 class=\"changes\"> Changes: </h2>";
echo "<div id=\"change_box\">";
foreach ($audit['Events'] as $event) {
if ( $event['Type'] != 'Comment') {
echo "<span class=\"field\">".$event['Field']."</span>";
echo $event['Value'];
echo "(".$event['Previous'] .")";
}
}
echo "</div>";
?>
please make space after each <? and make sure you enabled short tags
<?php if(is_array($audit['Events']) && $audit['Events']):?>
<h2 class="changes"> Changes: </h2>
<div id="change_box">
<?php foreach ($audit['Events'] as $event):?>
<?php if ( $event['Type'] != 'Comment'):?>
<span class="field">
<?php echo $event['Field'];?>
</span>:
<?php echo $event['Value']; ?>
<?php echo $event['Previous'];?>
<?php endif;?>
<?php endforeach;endif;?>
</div>

if table value does not exist echo text?

Please could someone explain to me how could I adapt my code to make it so that if a record / value doesnt exist in the mysql table it will echo a piece of text? Thank you.
<?php
$reviews_set = get_reviews();
?>
<h3>Latest Reviews</h3>
<?php
while ($reviews = mysql_fetch_array($reviews_set)) {
?>
<div class="prof-content-box" id="reviews">
<div class="message_pic">
<?php echo "<img width=\"50px\" height=\"50px\" src=\"{$prof_photo}\">";?>
<?php echo "<strong>Review from {$reviews['display_name']}:</strong><br /><br/> {$reviews['content']} <br />";
?>
Use the ternary operator '?:'
Sample:
$you_var ?: 'you_text_if_not_exists'
Check your variables like that :
<?php (isset($reviews['display_name']) ? $reviews['display_name'] : "entry doesn't exists"; ?>
<?php
$reviews_set = get_reviews();
?>
<h3>Latest Reviews</h3>
<?php
if(mysql_num_rows($reviews = mysql_fetch_array($reviews_set))>=1)
{
while ($reviews = mysql_fetch_array($reviews_set)) {
?>
<div class="prof-content-box" id="reviews">
<div class="message_pic">
<?php echo "<img width=\"50px\" height=\"50px\" src=\"{$prof_photo}\">";?>
<?php echo "<strong>Review from {$reviews['display_name']}:</strong><br /><br/> {$reviews['content']} <br />";
}
} else {
echo 'No reviews available';
}
?>
If you want to check if table has rows, use following:
$num_rows = mysql_num_rows($reviews_set);
$num_rows will contain number of rows.
<?php
$reviews_set = get_reviews();
?>
<h3>Latest Reviews</h3>
<?php
if(mysql_num_rows($reviews_set) > 0) {
while ($reviews = mysql_fetch_array($reviews_set)) {
?>
<div class="prof-content-box" id="reviews">
<div class="message_pic">
<?php echo "<img width=\"50px\" height=\"50px\" src=\"{$prof_photo}\">";?>
<?php echo "<strong>Review from {$reviews['display_name']}:</strong><br /><br/> {$reviews['content']} <br />";
<?
}
}else{
echo "No Data";
}
?>
I hope this helps

Categories