how to fetch data from database in four different divs? - php

i want to fetch out the 4 different id's rows from the database. and display in four divs. each div contains a character base unique id. i am doing this to write same query again and again. i want to do with less query.
my code is given below.
<?php
$query = mysql_query("select * from acms_pages where id = 40");
$sql = mysql_fetch_array($query);
?>
<li id="pgAbout">
<section class="content">
<div class="one_half_last"><?php echo $sql['text']; ?></div>
</section>
</li>
<section class="content">
<?php
$query = mysql_query("select * from acms_pages where id = 41");
$sql1 = mysql_fetch_array($query);
?>
<li id="pgPortfolio">
<div class="fullwidth"><?php echo $sql1['text']; ?></div>
</section>
</li>
<?php
$query = mysql_query("select * from acms_pages where id = 42");
$sql2 = mysql_fetch_array($query);
?>
<li id="pgServices">
<section class="content">
<div class="one_half_last"><?php echo $sql2['text']; ?></div>
</section>
</li>
<?php
$query = mysql_query("select * from acms_pages where id = 43");
$sql3 = mysql_fetch_array($query);
?>
<li id="pgPrices">
<section class="content">
<div class="one_half"><?php echo $sql3['text']; ?></div>
</section>
</li>

This probably belongs on programmers.stackexchange.com, but the general approach would be to rewrite your query as a single query, with the where clause including all four conditions OR'd together.
Then you can access each row independently and render the divs.

To answer your question (I think...) you can just use OR in your query:
mysql_query("select * from acms_pages where id = 40 OR id = 41 OR id = 42");
However, you should be using the mysqli instead:
$link = mysqli_connect("localhost", "my_user", "my_password", "mydb");
if ($result = mysqli_query($link, "select * from acms_pages where id = 40 OR id = 41 OR id = 42"))
{
printf("Select returned %d rows.\n", mysqli_num_rows($result));
while ($row = mysqli_fetch_row($result)) {
printf ("%s", $row[0]); //first column value here
}
/* free result set */
mysqli_free_result($result);
}

Related

php and mysql to create tabs with bootstrap 3

enter image description here
How can I make a loop with php where I separate the comments as rounds
All comments for the round 1
<div id="$row['round']">
$row['comments']
</div>
All comments for the round 2
<div id="$row['round']">
$row['comments']
</div>
but I can have many rounds, so I don't know the exact query to use
<div id="chat" class="chat">
<ul class="nav nav-tabs">
<?php
$stmt = $DB->query("SELECT * FROM messages WHERE $id = video_id GROUP BY round ORDER BY round DESC");
while ($row = $stmt->fetch()) {
echo '<li><a data-toggle="tab" href="#menu'.$row['round'].'">'.$row['round'].'</a></li>';
}
?>
</ul>
<div class="tab-content">
<?php
$i=-1;
$stmt = $DB->query("SELECT * FROM messages WHERE $id = video_id GROUP BY round ORDER BY round DESC");
while ($row = $stmt->fetch()) {
echo '<div class="tab-pane '.( $i = $i ? 'active' : '' ).'" id="menu'.$row['round'].'">';
$test = $row['round'];
$stmt2 = $DB->query("SELECT * FROM messages INNER JOIN system_users ON messages.user_id = u_userid WHERE $id = video_id AND $test = round ORDER BY date ASC");
while ($row2 = $stmt2->fetch()) { ?>
<br />
<p><b>round:</b> <?php echo $row2["round"]; ?></p>
<p><b>Message:</b> <?php echo $row2["message"]; ?></p>
<p><b>User:</b> <?php echo $row2["u_username"]; ?></p>
<p><b>time:</b> <?php echo date_format (new DateTime($row2["date"]), 'd|m|Y H:i:s'); ?></p>
<hr />
<?php
}$i =0;
echo '</div>';
}
?>
</div>
I am completely sure this is not the best solution, but it is working,
thanks anyway..

Dynamic Dropdown menu using PHP & mysql

I've been trying to build a dropdown menu but I'm not getting my desired results. Here's my code:
<?php require_once 'core/init.php'?>
<?php
$sql = 'SELECT * FROM categories WHERE parent = 0';
$pquery = mysqli_query($db,$sql);
?>
<?php while($parent = mysqli_fetch_assoc($pquery)):?>
<?php
$parent_id = $parent['id'];
$sql2 = 'SELECT * FROM categories WHERE parent = "parent_id"';
$cquery = mysqli_query($db,$sql2);
?>
<li class='dropdown'>
<a href='#' class='dropdown-toggle' data-toggle='dropdown'>
<?php echo $parent['id'];?><span class='caret'</span</a>
<ul class='dropdown-menu' role='menu'>
<?php while($child = mysqli_fetch_assoc($cquery)):>
<li><a href='#'><?php echo $child['parent'];?></a>
</li>
<?php endwhile; ?>
</ul>
</li>
<?php endwhile;?>
My DB is like this and the result is this.
Try with console phpmyadmin before write codes in page.
I think your codes is wrong.
Your code must like this
$parent_id = $parent['id'];
$sql2 = 'SELECT * FROM categories WHERE parent = '.$parent_id;
I suggest to you for use ajax to be beautiful again.

Pagination script added php page isn't loading or showing any error

I tried to add a pagination script to my existing php page of sql queries.
But after adding the script the page is kept on loading without showing any content or error.
My code goes as:
<?php include('db.php'); ?>
<?php // define how many results you want per page
$results_per_page = 10;
// find out the number of results stored in database
$sql10='SELECT * FROM smf_messages';
$result10 = mysqli_query($conn, $sql10);
$number_of_results = mysqli_num_rows($result10);
// determine number of total pages available
$number_of_pages = ceil($number_of_results/$results_per_page);
// determine which page number visitor is currently on
if (!isset($_GET['page'])) {
$page = 1;
} else {
$page = $_GET['page'];
}
// determine the sql LIMIT starting number for the results on the displaying page
$this_page_first_result = ($page-1)*$results_per_page;
?>
Now the sql query codes to get the data from the respective tables...
<?php
$sql2 = "SELECT * FROM smf_log_digest WHERE note_type = 'topic' ORDER BY id_msg DESC LIMIT 420";
$result2 = $conn->query($sql2);
if ($result2->num_rows > 0) {
while($row2 = $result2->fetch_assoc()) {
$number = $row2["id_msg"];
?>
This query relates to the content from which table to be retrieved..
<?php
// retrieve selected results from database and display them on page
$sql20='SELECT * FROM smf_messages WHERE id_msg = $number AND id_board = 4 LIMIT ' . $this_page_first_result . ',' . $results_per_page;
$result20 = mysqli_query($conn, $sql20);
while($row20 = mysqli_fetch_array($result20)) {
$member = $row20["id_member"];
$replies = $row20["id_topic"];
?>
<?php
$sqlstep1 = "SELECT COUNT(*) AS total FROM smf_log_digest WHERE note_type = 'reply' AND id_topic = $replies";
$rowNumstep1 = mysqli_query($conn, $sqlstep1);
$countstep1 = mysqli_fetch_assoc($rowNumstep1);
?>
// Body
<article class="well btn-group-sm clearfix">
<div class="topic-desc row-fluid clearfix">
<div class="col-sm-2 text-center publisher-wrap">
<img src="assets/images/profile.png" alt="" class="avatar img-circle img-responsive">
<h5><?php echo $row3["poster_name"]; ?></h5>
<small class="online">Member</small>
</div>
<div class="col-sm-10">
<header class="topic-footer clearfix">
<!-- end tags -->
</header>
<!-- end topic -->
<h4> <?php echo $row20["body"]; ?></h4>
<div class="blog-meta clearfix">
<small><?php echo $countstep1["total"]; ?> Replies</small>
<small><?php echo date('m/d/Y', $row20["poster_time"]); ?></small>
</div>
View the topic →
</div>
</div>
</article>
//end of body
<?php
}
// display the links to the pages
for ($page=1;$page<=$number_of_pages;$page++) {
echo '' . $page . ' ';
}
?>
<?php }
} else {
echo "";
}
?>
Please note that the data base connections are all checked and are right..
Any help is appreciated..
add this on top then check for error.
error_reporting(E_ALL);
ini_set('desplay_errors','1');

MySQL query doesn't show all the records correctly

The query am running against my database to get the 3 records order it by Random. The problem is that sometimes it shows all 3 records sometimes it only shows 2, 1 and other times its just blank. In the database I have around 28 records.
What I have tried
I have tried without LIMIT - Problem Same
I have echoed out $suggested_profile_id found all 3 records coming out.
This is the query that gets the records LIMIT it by 3
<?php
$sql = "SELECT * FROM members WHERE member_status='activated' ORDER BY RAND() DESC LIMIT 3";
$query = $db->SELECT($sql);
if($db->NUM_ROWS() > 0){
$rows = $db->FETCH_OBJECT();
?>
This is the code that runs and gets all 3 records in a loop.
<!-- Suggested Friends -->
<div class="col-md-0 media-body">
<?php
foreach($rows as $row){
$member_id = $row->member_id;
$sql = "SELECT * FROM profile WHERE profile_id='$member_id' LIMIT 1";
$query = $db->SELECT($sql);
$rows = $db->FETCH_OBJECT();
foreach($rows as $row){
$suggested_profile_id = $row->profile_id;
$suggested_profile_photo = $row->profile_photo;
$suggested_profile_username = $row->profile_username;
$suggested_profile_name = $row->profile_name;
if(
$suggested_profile_id != GET_SESSION_ID_VALUE(ENCRYPTION_KEY)&&
!is_in_ARRAY($make_string_to_ARRAY, $suggested_profile_id)
){
?>
<div class="row margin0">
<div class="col-md-4 pad0">
<a href="/<?php echo $suggested_profile_username; ?>" title="<?php echo $suggested_friends_profile_name; ?>" >
<?php
global $suggested_friends_profile_id;
$member_dir = dirname(dirname(dirname(__FILE__))) . "/members/" . $suggested_profile_id ."/smalll_" . $suggested_profile_photo;
if(file_exists($member_dir)){
?>
<img alt="<?php echo $suggested_profile_name; ?>" title="<?php echo $suggested_profile_name; ?>" src="/members/<?php echo $suggested_profile_id; ?>/smalll_<?php echo $suggested_profile_photo; ?>" width="50" height="50">
<?php
} else {
?>
<img alt="<?php echo $suggested_profile_name; ?>" title="<?php echo $suggested_profile_name; ?>" src="/assets/images/default.jpg" width="50" height="50">
<?php
}
?>
</a>
</div>
<div class="col-md-8 pad0">
<?php echo $suggested_profile_name; ?>
<span class="f12 gray">271 Mutual Friends</span>
Add as friend
</div>
</div>
<?php
}
}
}
?>
</div>
<!-- ** Suggested Friends -->
What am I missing? Is there any alternative way I can achieve this...thanks!
It looks to me like you're overwriting your $rows variable within the inner select.
foreach($rows as $row){ // <-- first $rows / $row
$member_id = $row->member_id;
$sql = "SELECT * FROM profile WHERE profile_id='$member_id' LIMIT 1";
$query = $db->SELECT($sql);
$rows = $db->FETCH_OBJECT(); <-- $rows overwritten
foreach($rows as $row){
Break your display from your application logic and you won't have such a hard time debugging this kind of thing. Besides, you have a lot of duplicated code and that makes things hard to manage as well as being hard to debug.
Further, you wouldn't have this problem if you ran one query: SELECT * FROM members JOIN profile ON members.member_id = profile.profile_id and not only does your code get simpler and your double-foreach loop problem disappear, but your database access will also be a lot more efficient.

Dynamic Dropdown menu - PHP

First of all, I am new to mysqli and prepare statements so please let me know if you see any error. I have this static drop down menu :
HTML code:
<ul class="menu sgray fade" id="menu">
<li>Bike
<!-- start mega menu -->
<div class="cols3">
<div class="col1">
<ol>
<li>bikes</li>
<li>wheels</li>
<li>helmets</li>
<li>components</li>
</ol>
</div>
<div class="col1">
<ol>
<li>pedals</li>
<li>GPS</li>
<li>pumps</li>
<li>bike storage</li>
</ol>
</div>
<div class="col1">
<ol>
<li>power meters</li>
<li>hydratation system</li>
<li>shoes</li>
<li>saddles</li>
</ol>
</div>
</div>
<!-- end mega menu -->
</li>
I want to make a dynamic dropdown menu. I managed to show the $categoryName and the $SubCategoryName with this function:
function showMenuCategory(){
$db = db_connect();
$query = "SELECT * FROM Category";
$stmt = $db->prepare($query);
$stmt->execute();
$stmt->bind_result($id,$categoryName,$description,$pic,$active);
while($stmt->fetch()) {
echo'<li>'.$categoryName.'
<!-- start mega menu -->
<div class="cols3">
<div class="col1">
<ol>';
$dba = db_connect();
$Subquery = "SELECT * FROM Subcategory WHERE CategoryId = '".$id."'";
$Substmt = $dba->prepare($Subquery);
$Substmt->execute();
$Substmt->bind_result($Subid,$CatId,$SubCategoryName,$SubDescription);
while($Substmt->fetch()) {
echo'
<li>'.$SubCategoryName.'</li>';
}
echo'
</ol>
</div>
<!-- end mega menu -->
</li>';
}
}
The only problem is that it returns all the subcategories on the the same <div class="col1">:
what I would like to obtain is count the subcategories and if the result is more than 4 return the other items in the second and third column.
UPDATE***: thanks to the answer below now the menu looks like this:
thanks!
How about try this?
To explain further
What is happening is that for every subcategory fetched, I increment a counter. If that counter hits 4, it ends the <UL> and <DIV> and creates a new one which will represent the new column.
function showMenuCategory(){
$db = db_connect();
$query = "SELECT * FROM Category";
$stmt = $db->prepare($query);
$stmt->execute();
$stmt->bind_result($id,$categoryName,$description,$pic,$active);
while($stmt->fetch()) {
echo'<li>'.$categoryName.'
<!-- start mega menu -->
<div class="cols3">
<div class="col1">
<ol>';
$dba = db_connect();
$Subquery = "SELECT * FROM Subcategory WHERE CategoryId = '".$id."'";
$Substmt = $dba->prepare($Subquery);
$Substmt->execute();
$Substmt->bind_result($Subid,$CatId,$SubCategoryName,$SubDescription);
$count = 0;
while($Substmt->fetch()) {
echo'
<li>'.$SubCategoryName.'</li>';
$count+=1;
if ($count == 4) {
$count = 0;
echo '</ol></div><div class="col1"><ol>';
}
}
echo'
</ol>
</div>
<!-- end mega menu -->
</li>';
}
}
EDIT: Misunderstood the purpose of col1. They all should be col1 and should work now. If not, leave me a comment!
Try this:
function showMenuCategory(){
$db = db_connect();
$query = "SELECT * FROM Category";
$stmt = $db->prepare($query);
$stmt->execute();
$stmt->bind_result($id,$categoryName,$description,$pic,$active);
echo '<div class="cols3">';
while($stmt->fetch()) {
echo'<li>'.$categoryName.'
<!-- start mega menu -->
<div class="col1">
<ol>';
$dba = db_connect();
$Subquery = "SELECT * FROM Subcategory WHERE CategoryId = '".$id."'";
$Substmt = $dba->prepare($Subquery);
$Substmt->execute();
$Substmt->bind_result($Subid,$CatId,$SubCategoryName,$SubDescription);
while($Substmt->fetch()) {
echo'<li>'.$SubCategoryName.'</li>';
}
echo'</ol>';
}
echo '</div><!-- end mega menu --></li>';
}

Categories