when no rows retrieved from SQL database then dont display a div - php

i tried the empty selector in jquery but it doesnt work. the content is still being displayed. i am retrieving some rows from the SQL database.. if there is no database then i dont want to display that div.
<div id="scrollingText">
<div class="scrollWrapper">
<div class="scrollableArea">
<marquee behavior="scroll" direction="left">
<p>
<?php
$con = mysql_connect("localhost","fraptech_test","");
mysql_select_db("fraptech_test", $con);
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysql_select_db("fraptech_ndsnotice", $con);
$result = mysql_query("SELECT * FROM ndsnotice");
while($row = mysql_fetch_array($result))
{
echo $row['Notice'];
}
?>
</p>
</marquee>
</div>
</div>
</div>

Is that your whole code? Are you working with ajax? If you're using pure PHP without ajax just set the output div into your "if"-conditions?
If you want to use jQuery, try:
if ( ($("div.scrollableArea p").text()).length > 0 )
{
$("div.scrollableArea p").show();
}
else
{
$("div.scrollableArea p").hide();
}
But you can do it without jQuery, I guess.

You need to put the HTML inside your php if clause.
Simple example:
<div id="scrollingText">
<?php
$con = mysql_connect("localhost","fraptech_test","");
mysql_select_db("fraptech_ndsnotice", $con);
$result = mysql_query("SELECT * FROM ndsnotice");
if (mysqli_connect_errno($con)) {
<div class="scrollWrapper">
<div class="scrollableArea">
<marquee behavior="scroll" direction="left">
<p>
<?php
while($row = mysql_fetch_array($result))
{
echo $row['Notice'];
}
?>
</p>
</marquee>
</div>
</div>
<?php } else { ?>
<div class="errordiv">Display this on error</div>
<?php } ?>
</div>

thank you guys with ur help i made some change. the below code did it:
<?php
$con = mysql_connect("localhost","fraptech_test","");
mysql_select_db("fraptech_test", $con);
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysql_select_db("fraptech_ndsnotice", $con);
$result = mysql_query("SELECT * FROM ndsnotice");
if (mysql_num_rows($result) > 0)
{
?><div id="scrollingText">
<div class="scrollWrapper">
<div class="scrollableArea">
<marquee behavior="scroll" direction="left">
<p>
<?php while($row = mysql_fetch_array($result))
{
echo $row['Notice'];
}
?> </p>
</marquee>
</div>
</div>
</div>
<?php } ?>

Related

How to echo values in HTML that are coming from mysqli_multi_query in php

I am able to get values displayed from database when using mysqli_multi_query.But I am not able to print in my HTML code.Its Probably the issue with while loop. Below is my code which gives me the AVG for the three SQL statements. Only problem is with displaying average value in HTML.
<?php
$con = new mysqli("localhost", "root","","survey");
if ($con){ printf(""); }
$query = "SELECT AVG(workarea) FROM score;" ;
$query .= "SELECT AVG(manager) FROM score;";
$query .= "SELECT AVG(comm) FROM score;";
// Execute multi query
if (mysqli_multi_query($con,$query)) {
do {
if ($result=mysqli_store_result($con)) {
// Fetch one and one row
while ($row=mysqli_fetch_row($result)) {
printf("%s\n",$row[0]);
}
// Free result set
mysqli_free_result($result);
}
} while (mysqli_next_result($con));
}
?>
<div class="row count">
WORK AREA/UNIT SCORE : <?php echo $row[0]; ?> // echo $row[AVG(workarea)] doesnot work
</div>
<div class="row count">
SUPERVISOR/MANAGER SCORE : <?php echo $row[0]; ?> // echo $row[AVG(manager)]
</div>
<div class="row count">
COMMUNICATION SCORE : <?php echo $row[0]; ?> // echo $row[AVG(comm)] doesnot work
</div>
No need of multi query you just get AVG in single query. And write your html inside while loop as
$con = new mysqli("localhost", "root", "", "survey");
if ($con) {
printf("");
}
$query = "SELECT AVG(workarea),AVG(manager),AVG(comm) FROM score;";
// Execute multi query
if ($result = $con->query($query)) {
/* fetch object array */
while ($row = $result->fetch_row()) {
?>
<div class="row count">
WORK AREA/UNIT SCORE : <?php echo $row[0]; ?>
</div>
<div class="row count">
SUPERVISOR/MANAGER SCORE : <?php echo $row[1]; ?>
</div>
<div class="row count">
COMMUNICATION SCORE : <?php echo $row[2]; ?>
</div>
<?php
}
/* free result set */
$result->close();
}

Repeat div width with Different Database Table ID

I am trying to repeat a div the same div multiple times with as many rows as i have in the database table. a non working example i made of what i want is below.
<?php for ($i=0; $i < 3; $i++) {
?><!-- switch the three with the number of rows in the table-->
I would want to loop the container and switch the database query with the value of i.
<div id = "bodyContainer">
<form class="review">
<div class="reviewContainer">
<div class = "images">
<?php
$link = mysqli_connect("localhost", "root", "root","DJP");
if (mysqli_connect_error()) {
die("Could not connect to database");
}
$query = "SELECT * FROM reviews WHERE id = '<?php echo $i ?><!--switch the value of i here-->'";
$result=mysqli_query($link, $query);
$row = mysqli_fetch_array($result);
echo '<img height = 90% width = 100% src="data:image/jpeg;base64,'.$row[3].' " />';
?>
</div>
<!--<div class="title">
<?php
//echo $row[1];
?>
</div>-->
<div class = "comment">
<?php
echo $row[2];
?>
</div>
<div class = "rating" style = "float: right;">
<?php
echo $row[4];
?>
<br/>
<br/>
stars
</div>
</div><!--end review-->
</form>
</div><!--end body container-->
<?php;}?><!-- end the for loop so all the divs can be re-done with a new entry for the query. I would move the $link out of loop also.-->
Just fetch all the rows once and then go through them as you print the data:
$link = mysqli_connect("localhost", "root", "root","DJP");
if (mysqli_connect_error()) {
die("Could not connect to database");
}
$query = "SELECT * FROM reviews";
$result=mysqli_query($link, $query);
while($row = mysqli_fetch_array($result)){ ?>
<div id = "bodyContainer">
<form class="review">
<div class="reviewContainer">
<div class = "images">
<?php
echo '<img height = 90% width = 100% src="data:image/jpeg;base64,'.$row[3].' " />';
?>
</div>
<!--<div class="title">
<?php
//echo $row[1];
?>
</div>-->
<div class = "comment">
<?php
echo $row[2];
?>
</div>
<div class = "rating" style = "float: right;">
<?php
echo $row[4];
?>
<br/>
<br/>
stars
</div>
</div><!--end review-->
</form>
</div><!--end body container-->
<?php } ?>

How to create jquery tabs using php?

I used jquery-ui tabs using php but it's not working, I am unable to identify the problem, please help me.
<div id="tabs">
<div class="row">
<div class="col-md-4">
<?php
$con = mysqli_connect("localhost", "admin", "123456", "gazette");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM gazette_details");
while ($row = mysqli_fetch_array($result)) {
echo "<ul>";
$id = '#tabs-' . $row['id'];
echo "<li>" . "<a href='$id'>" . $row['gazette_id'] . "</a>" . "</li>";
echo "</ul>";
}
mysqli_close($con);
?>
</div>
<div class="col-md-4">
<div id="tabs-12">1</div>
<div id="tabs-13">2</div>
<div id="tabs-14">3</div>
</div>
</div>
<div class="col-md-4">.col-md-4</div>
</div>
I used this website example: http://jqueryui.com/tabs/
you may try
<?php
$ARR_RESULT = array();
$con = mysqli_connect("localhost", "admin", "123456", "gazette");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM gazette_details");
while ($row = mysqli_fetch_array($result)) {
$ARR_RESULT[] = $row;
}
mysqli_close($con);
?>
<div id="tabs">
<ul>
<?php
foreach($ARR_RESULT as $arr)
{
echo '<li>'.$arr['gazette_id'].'</li>';
}
?>
</ul>
<?php
foreach($ARR_RESULT as $arr)
{
echo '<div id="tabs-'.$arr['id'].'">';
echo '<p>Tab-'$arr['id'].'</p>';
echo '</div>';
}
?>
</div>

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>
';
}

While Statement and Divs

I am trying to get the code below to loop using a while statement. The problem I am having is getting the divs to worth with in the PHP. Any ideas on how I can make this happen? My database consists of a columns called month, day, description, and location.
<div class="events-module">
<?php
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("sandbox", $con);
$result = mysql_query("SELECT * FROM events"); ?>
<?php while($row = mysql_fetch_array($result)) ?>
<div class="date">
<div id="month"><h4><?php echo $row['month']; ?></h4></div>
<div id="day"><h3><?php echo $row['day']; ?></h3></div>
</div>
<div class="event">
<div id="description"><p><?php echo $row['description']; ?></p></div>
<div id="location"><p><b><?php echo $row['location']; ?></b></p></div>
</div>
<?php endwhile;
mysql_close($con);
?>
</div>
You didn't say exactly what's wrong. But, it seems you are missing your start block for your while loop
while (.... ) :
^^^
... // div stuff
end while

Categories