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>
';
}
Related
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();
}
Overall I would like to echo all elements of the DB table 'products' as buttons on a row, and for each 10th element, a lineshift should be implemented. Each line should be surrounded by a specific ().
I have printed the code I have been working with to do this:
products_backend.php:
$stmt = $db->prepare("SELECT * FROM products");
$stmt->execute();
$count = $stmt->rowCount();
if($count > 1){
$table_row = $stmt->fetchAll();
$i = 0;
foreach($table_row as $data) {
$i++;
?>
<div class="col-md-1 cash_register pants">
<button class="table_add" value="Bukser">
<h4><?php echo $data['product_name']; ?></h4>
</button>
</div>
<?php
if ($i%10 == 0):
echo "<br>";
endif;
}
}
And then I have the "products.php page", where the extracted data is shown:
<div class="container-fluid">
<section class="wrapper">
<div class="row">
<div class="row-eq-height">
<?php require("products_backend.php"); ?>
</div>
</div>
</section>
</div>
But the output of this is all the products in the DB printed on one line, without any lineshift for every 10th line:
The image shows the output, however due to the window size, only 11 products are shown, but i keeps going outside the window.
To summarize, I want the output to insert a lineshift for every 10th product printed, and that each row should be inside the div, like in the manipulated Picture below: (Notice, that in order to create the Picture, I have just copied the output from the row line in order to create the second row, Thus I do not want the same output in the second row as in the first row)
So what am I doing wrong in my code above?
the class "col-md-1" looks like a floating div
so to break this floating divs, try this :
echo "<br style=\"clear : both;\">";
You can use % moduler to add div for after every 10 record;
Updated code :
$stmt = $db->prepare("SELECT * FROM products");
$stmt->execute();
$count = $stmt->rowCount();
if($count > 1){
$table_row = $stmt->fetchAll();
$i = 0;
foreach($table_row as $data) {
if(($i%10) == 0){ echo "<div class="clearfix"></div>";}
?>
<div class="col-md-1 cash_register pants">
<button class="table_add" value="Bukser">
<h4><?php echo $data['product_name']; ?></h4>
</button>
</div>
<?php
$i++;
}
}
Try this:
$stmt = $db->prepare("SELECT * FROM products");
$stmt->execute();
$count = $stmt->rowCount();
if($count > 1){
$table_row = $stmt->fetchAll();
$i = 1;
echo "<div>";
foreach($table_row as $data) {
?>
<div class="col-md-1 cash_register pants">
<button class="table_add" value="Bukser">
<h4><?php echo $data['product_name']; ?></h4>
</button>
</div>
<?php
if(($i%10) == 0){ echo "</div><div>";}
$i++;
}
echo "</div>";
}
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
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 have a database that has two fields: current, and previous.
Here is the code:
<?
$username="*****";
$password="*****";
$database="******";
mysql_connect(localhost,$username,$password) or die("Unable to connect to server");
mysql_select_db($database) or die( "Unable to select database");
$query='SELECT * FROM tasks WHERE current=1 OR previous=1';
$result=mysql_query($query);
$num=mysql_num_rows($result);
mysql_close();
?>
It then output it and manipulate it like this:
<div class="tasklist">
<div class="currentProject">
Current Project:
</div>
<?
$i=0;
while ($i < $num) {
$title=mysql_result($result,$i,"Title");
$link=mysql_result($result,$i,"Weblink");
$description=mysql_result($result,$i,"Description");
$id=mysql_result($result,$i,"ID");
$howto=mysql_result($result,$i,"howto");
$blogpost=mysql_result($result,$i,"blogpost");
$done=mysql_result($result,$i,"done");
$current=mysql_result($result,$i,"current");
$previous=mysql_result($result,$i,"previous");
if ( $current == 1 ) {
$current = "current";
} else {
$current = "";
}
if ( $previous == 1 ) {
$previous = "previous";
} else {
$previous = "";
}
?>
<div class="<? echo $done ?> task <? echo $id ?>" id="<? echo $current.$previous ?>">
<div class="titleWrapper">
<div class="title">
<a class="article" href="<? echo $link ?>"><? echo $title ?></a>
</div>
</div>
</div><BR>
<?
$i++;
}
echo "</div>";
?>
The problem is that since previous comes before current in the database, it outputs previous and then current.
Question:
How do I make sure to output current first, and then previous?
Instead of outputting both directly, store them until you actually have both, and then output both in the correct order.
Just append this to your query order by previous.
It must work as long as current task registry has previous=0 and previous task registry has previous=1.