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
Related
code:
<!-- content -->
<article id="content" class="tabs">
<div class="wrapper">
<div class="box2">
<?php
//session_start();
$sql = "SELECT * FROM `db`.`news` ORDER BY `date` DESC LIMIT 20";
include 'php/dbconnection.php';
$conn = OpenCon(); //$conn=$_SESSION['conn'];
$result = mysqli_query($conn,$sql);
if($result){
$rows = mysqli_fetch_array($result);
echo "<div class='wrapper tab-content'>";
echo "<section class='col1'>";
echo "<h4><span>" . $rows['date']. "</span> </h4>";
echo "<p class='pad_bot2'><strong>". $rows['title']. "
</strong></p>";
echo "<p class='pad_bot1'>".$rows['des'] ."</p>";
echo "</section>";
echo "</div>";
}
?>
</div>
</div>
</article>
<!-- /content-->
Here I'm trying to, print 'news' table from database, but I'm unable to print anything on webpage.
can anyone point out, what is wrong in this ?
Thank You in advance for any help!
You have to put mysqli_fetch_row($result) inside a loop.
Example with your case:
if($result){
while ($rows=mysqli_fetch_row($result)){
echo "<div class='wrapper tab-content'>";
echo "<section class='col1'>";
echo "<h4><span>" . $rows['date']. "</span> </h4>";
echo "<p class='pad_bot2'><strong>". $rows['title']. "
</strong></p>";
echo "<p class='pad_bot1'>".$rows['des'] ."</p>";
echo "</section>";
echo "</div>";
}
}
// Free result set
mysqli_free_result($result);
Hope this helps!
For commented $conn=$_SESSION['conn'];
You cannot store resource handles within a $_SESSION. See PHP.net:
Some types of data can not be serialized thus stored in sessions. It
includes resource variables or objects with circular references (i.e.
objects which passes a reference to itself to another object).
As your object contains the connection link it can't be serialized, Just reconnect with every request.
And you can make clean template like below
<!-- content -->
<article id="content" class="tabs">
<div class="wrapper">
<div class="box2">
<?php
//session_start();
$sql = "SELECT * FROM `db`.`news` ORDER BY `date` DESC LIMIT 20";
include 'php/dbconnection.php';
$conn = OpenCon();
$result = mysqli_query($conn,$sql);
if($result):
while ($row=mysqli_fetch_row($result)):
?>
<div class='wrapper tab-content'>
<section class='col1'>
<h4><span><?php echo $row['date'];?></span></h4>
<p class='pad_bot2'>
<strong><?php echo $row['title'];?></strong>
</p>
<p class='pad_bot1'><?php echo $row['des'];?></p>
</section>
</div>
<?php
endwhile;
else:
?>
<p>No active news</p>
<?php
endif;
?>
</div>
</div>
</article>
<!-- /content-->
// Numeric array
$row=mysqli_fetch_array($result,MYSQLI_NUM);
// Associative array
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
So for your code, you need to change:
$rows = mysqli_fetch_array($result);
to:
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
Also, you should check for number of rows before fetching by using:
$rowcount=mysqli_num_rows($result);
Only if $rowcount is more than 0, you should mysqli_fetch_array.
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();
}
I have a problem with my search.php file to render me results...
I dont get any strings of error, but when I type an existing keyword I get no results...
The format of the results are the same as viewed in my main content on the website (grid view)...
The code:
<body>
<?php include_once("analyticstracking.php") ?>
<div class='container'> <!--Start of the container-->
<div><?php include("includes/header.php"); ?></div>
<div><?php include("includes/navbar.php"); ?></div>
<div><?php include("includes/left_col.php"); ?></div>
<div class='main_col'>
<div class='main_content'>
<?php
include("includes/connect.php");
if(isset($_GET['search'])){
$search_id = $_GET['q'];
$search_query = "SELECT * FROM games WHERE game_keywords LIKE '%$search_id%'";
$run_query = mysql_query($search_query);
echo '<table>';
$games = 0;
while($search_row = mysql_fetch_array($run_query)){
// make a new row after 9 games
if($games%9 == 0) {
if($games > 0) {
// and close the previous row only if it's not the first
echo '</tr>';
}
echo '<tr>';
}
// make a new column after 3 games
if($games%3 == 0) {
if($games > 0) {
// and only close it if it's not the first game
echo '</td>';
}
echo '<td>';
}
$game_id = $search_row['game_id'];
$game_name = $search_row['game_name'];
$game_category = $search_row['game_name'];
$game_keywords = $search_row['game_name'];
$game_image = $search_row['game_image'];
?>
<div class="game_grid">
<a href="game_page.php?id=<?php echo $game_id; ?>"><img src="images/games_images/<?php echo $game_image; ?>" width="120" height="120" />
<span><?php echo $game_name; ?></span>
</div>
<?php
$games++;
}
}
?>
</table>
</div>
</div>
<div><?php include("includes/footer.php"); ?></div>
</div> <!--End of the container-->
</body>
Any idea?
EDIT:
I solved my problem, its a small mistake I made,
In the HTML form of the search I forgot to give the submit button: "name="search", I removed it accidently... now everything works perfectly :)
You have a typo in code
change code as below
if(isset($_GET['search'])){
$search_id = $_GET['search']; //$_GET['q'];
.
.
.
}
I solved my problem, its a small mistake I made, In the HTML form of the search I forgot to give the submit button: "name="search", I removed it accidentally... now everything works perfectly :)
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";
}
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>
';
}