how can i use Ajax and Jquery to my code - php

i am very new to Ajax and jquery.
I have already post my queston here
How can i use ajax with javascript function
but not clear to me,so i have paste my all code.
i am using the following javascript function to get the value from the list those user selected from list and pass this value to sql query to display products.
i am trying to reaload only products.
but using this function each time the page is reloading. i am trying to use ajax using this function.how can i use ajax with this need syntax.
In this 3 things Reaload my whole page are 1)Search form 2)sort by 3)view on page(limit)
<?php
/*
Template Name:Wheel
*/
?>
<?php get_header(); ?>
<script type="text/javascript" language="javascript">
function pagelim(index){
var page_lim=$('#page_num li').get(index).id;
$("#main-content-wrap").load("<?php echo get_option('head'); ?>"+'?wheel&limit=' + page_lim);
}
</script>
<script type="text/javascript" language="javascript">
function dateby(index){
var date_by=$('#sort-by-date a').get(index).id;
var cls=document.getElementById(date_by).className;
if(date_by=="ASC")
{
date_by="DESC";
}
else
{
date_by="ASC";
}
self.location="<?php echo get_option('head'); ?>"+'?wheel&sort=' + date_by ;
}
</script>
<script type="text/javascript" language="javascript">
onLoad(){
//search-option_form.removeClass('jqtransformdone');
}
</script>
<?php
$sort_by=$_GET['sort'];
if($sort_by=="")
{
$sort_by="ASC";
}
else
{
$sort_by=="DESC";
}
$page = (get_query_var('page')) ? get_query_var('page') : 1;
$lim=$_GET['limit'];
if($lim=="")
$limit = 1;
else
$limit =$lim;
$offset = ( $page - 1 ) * $limit;
if(isset($_POST['searchsubmit']))
{
$i=1;
$search_query="SELECT * FROM wp_products WHERE 1";
if($_POST['rim_size']!=0)
{$search_query=$search_query." AND rim='".$_POST['rim_size']."'"." ";}
if($_POST['studs']!=0)
{$search_query=$search_query." AND studs='".$_POST['studs']."'"." ";}
if($_POST['miles']!=0)
{
$value=$_POST['miles']-5000;
$search_query=$search_query." AND miles BETWEEN $value AND ". $_POST['miles']." ";
}
if(($_POST['min_price']!=0)&&($_POST['max_price']!=0))
{
$search_query=$search_query." AND "."price BETWEEN ". $_POST['max_price']." AND ".$_POST['min_price']." ";
}
$search_query=$search_query." LIMIT $offset,$limit";
$data=$wpdb->get_results($search_query);
}//if ends
else{$data = $wpdb->get_results("select * from wp_products order by product_id $sort_by
LIMIT $offset,$limit ");}
?>
<div class="main noborder" >
<div id="main-content-wrap">
<!-- - - - - - - - - - - - - - - Container - - - - - - - - - - - - - -
- - -->
<section class="container sbr clearfix left-section">
<!-- - - - - - - - - - - - - - - Content - - - - - - - - - - - - - - -
- -->
//here is search box
<div class="widget_custom_search">
<h3 class="widget-title"><span>Search</span> Filters</h3>
<form method="post" id="search-option-form" class="form-panel">
<fieldset>
<label>Rim Size</label>
<select name="rim_size" class="width-190">
<option value="0" >Select</option>
<option value="15" <?php if($_POST['rim_size']=="15") { ?> selected="selected" <?php } ?>>15 Inch</option>
<option value="16" <?php if($_POST['rim_size']=="16") { ?> selected="selected" <?php } ?>>16 Inch</option>
<option value="17" <?php if($_POST['rim_size']=="17") { ?> selected="selected" <?php } ?>>17 Inch</option>
<option value="18" <?php if($_POST['rim_size']=="18") { ?> selected="selected" <?php } ?>>18 Inch</option>
<option value="19" <?php if($_POST['rim_size']=="19") { ?> selected="selected" <?php } ?>>19 Inch</option>
</select>
</fieldset>
<fieldset>
<label>No. of Studs</label>
<select name="studs" class="width-190">
<option value="0">Select</option>
<option value="3" <?php if($_POST['studs']=="3") { ?> selected="selected" <?php } ?> >3</option>
<option value="4" <?php if($_POST['studs']=="4") { ?> selected="selected" <?php } ?> >4</option>
</select>
</fieldset>
<fieldset>
<span class="w110p floatLeft display-block">
<label>Min Price</label>
<select name="min_price" class="floatLeft">
<option value="0">Select</option>
<option value="1000" <?php if($_POST['min_price']=="1000") { ?> selected="selected" <?php } ?> >1000</option>
<option value="2000" <?php if($_POST['min_price']=="2000") { ?> selected="selected" <?php } ?>>2000</option>
['min_price']=="6000") { ?> selected="selected" <?php } ?>>6000</option>
</select>
</span> <span class="w110p floatLeft display-block">
<label>Max Price</label>
<select name="max_price" class="floatLeft">
<option value="0">Select</option>
<option value="1000" <?php if($_POST['max_price']=="1000") { ?> selected="selected" <?php } ?>>1000</option>
<option value="2000" <?php if($_POST['max_price']=="2000") { ?> selected="selected" <?php } ?>>2000</option>
<option value="3000" <?php if($_POST['max_price']=="3000") { ?> selected="selected" <?php } ?>>3000</option>
</select>
</span>
</fieldset>
<fieldset>
<label>Miles</label>
<select name="miles" class="width-190">
<option value="0">Select</option>
<option value="5000" <?php if($_POST['miles']=="5000") { ?> selected="selected" <?php } ?>>5000</option>
<option value="10000" <?php if($_POST['miles']=="10000") { ?> selected="selected" <?php } ?>>10000</option>
<option value="60000" <?php if($_POST['miles']=="60000") { ?> selected="selected" <?php } ?>>60000</option>
<option value="65000" <?php if($_POST['miles']=="65000") { ?> selected="selected" <?php } ?>>65000</option>
<option value="70000" <?php if($_POST['miles']=="70000") { ?> selected="selected" <?php } ?>>70000</option>
</select>
</fieldset>
<fieldset class="top-padding-30">
<button name="searchsubmit" type="submit" id="searchsubmit" class="common-button green-button-bg w90p floaL mLeft5">SEARCH</button>
</fieldset>
</form>
<!--/ .form-panel-->
</div>
</section>
<!--/.container -->
<!-- - - - - - - - - - - - - end Container - - - - - - - - - - - - - -
- - -->
<section class="container clearfix right-section">
<div id="pagination-sort-lniks">
<?php
$total=$wpdb->get_results("select * from wp_products ");
$pages = COUNT($total);
$pages = ceil($pages / $limit);
$querystring = "";
foreach ($_GET as $key => $value) {
if ($key != "page") $querystring .= "$key=$value&";
}
// Pagination
?>
<?php if($sort_by=="ASC")
$class="up-arrow";
else
$class="down-arrow";
//here to select sort by and view on page ?>
<div class="sort-links"><span class="by-date" id="sort-by-date">Sort by: <a href="#" id='<?php _e($sort_by)?>' class='<?php _e($class)?>' onclick="dateby($(this).index())" >Date</a>
</span>
<span id="view-on-page">View on Page: <?php if($lim=="") { _e($limit); } else { _e($lim); } ?>
<ul id="page_num">
<li id="5" onclick="pagelim($(this).index())">5</li>
<li id="10" onclick="pagelim($(this).index())">10</li>
<li id="15" onclick="pagelim($(this).index())">15</li>
</ul>
</span>
</div>
<div class="pagination">
<?php
if( $pages > 1)
{
$range=1;
$showitems = ($range * 2)+1;
$page1=$page;
$prev=$page1-1;
if($page > 1)
{
echo "<a class=\"page gradient\" ";
echo "href=\"?{$querystring}page=$prev";
echo "\">Previous</a> ";
}
for ($i = 1; $i <= $pages; $i++)
{
if (1 != $pages &&( !($i >= $page+$range+1 || $i <= $page-$range-1) || $pages <= $showitems ))
{
if($i == $page)
{
echo "<span class=\" page active\">".$i."</span>";
}
else
{
echo "<a class=\"page gradient\"";
echo "href=\"?{$querystring}page=$i";
echo "\">$i</a> ";
}
}
}
if($page!=$pages)
{
if($showitems < $pages)
{
echo "..... ";
}
$page1=$page;
$next=$page1+1;
echo "<a " . ($i == $page ? "class=\"page active\" " : "class=\"page gradient\"");
echo "href=\"?{$querystring}page=$next";
echo "\">Next</a> ";
}
}
?>
</div>
</div>
<?php
$tim_path = get_bloginfo('template_directory')."/scripts/timthumb.php?src=";
if(count($data)>0)
{
foreach($data as $row)
{
$userid = $row->dealer_id;
$metakey="addr1";
$prod_id = $row->product_id;
$image=$wpdb->get_results("select DISTINCT(product_id),prod_image from wp_product_images where product_id=$prod_id LIMIT 1");
foreach($image as $row1)
{
$image_name = $row1->prod_image;
}
?>
<?php $location=get_usermeta( $userid, $metakey ); ?>
<ul class="product-wrap" id="product-wrap">
<li><a href="#" class="current details-link" ><img src="<?php _e($tim_path) ?><?php bloginfo('template_directory');?>/product_images/<?php _e($image_name."&w=140&h=105&zc=0&watermark=0") ?>" class="thumb" alt=""/>
<?php if($row->bestdeal==1)
{?>
<span class="best-deal-icon right100" ></span> <?php }?>
</a>
<div class="product-title clearfix">
<h3 class="textBold"><?php _e($row->product_name) ?></h3>
<span class="product-price bTextColor"><?php _e($row->price)?></span> </div>
<div class="tech-details"> <span class="span1">Technical details : </span> <span class="span2"><?php _e($row->rim) ?>"</span> <span
class="span3"><?php _e($row->studs) ?>Studs</span> <span class="span4"><?php _e($row->colour) ?></span>
<span class="span5"><?php _e($row->miles) ?>Mil</span> </div>
<div class="location-details" style=""> <span class="location-display">Location : <?php _e($location) ?> </span> <span
class="view-details">View details </span> </div>
</li>
</ul>
<?php }
}
else
{
echo '<div class="no_records">Sorry!!No Products Found You Can Search Another. </div>';
}
?>
<!--/ .adjective-->
<div id="pagination-sort-lniks">
<div class="pagination">
<?php
if( $pages > 1)
{
$range=1;
$showitems = ($range * 2)+1;
$page1=$page;
$prev=$page1-1;
if($page > 1)
{
echo "<a class=\"page gradient\" ";
echo "href=\"?{$querystring}page=$prev";
echo "\">Previous</a> ";
}
for ($i = 1; $i <= $pages; $i++)
{
if (1 != $pages &&( !($i >= $page+$range+1 || $i <= $page-$range-1) || $pages <= $showitems ))
{
if($i == $page)
{
echo "<span class=\" page active\">".$i."</span>";
}
else
{
echo "<a class=\"page gradient\"";
echo "href=\"?{$querystring}page=$i";
echo "\">$i</a> ";
}
}
}
if($page!=$pages)
{
if($showitems < $pages)
{
echo "..... ";
}
$page1=$page;
$next=$page1+1;
echo "<a " . ($i == $page ? "class=\"page active\" " : "class=\"page gradient\"");
echo "href=\"?{$querystring}page=$next";
echo "\">Next</a> ";
}
}
?>
</div>
</div>
</section>
</div>
</div>
<!--/ .main-->
<?php get_footer();?>

With Jquery/Ajax the execution flow is different, let me try to explain:
PHP is executed on the server, after the execution the result is pure HTML+CSS+javascript. This result is then send to the client, where the browser will display it.
So what you are doing here is calling the same PHP multiple times, and depending of the variables you pass to it you generate diferent HTML+CSS+javascript results.
So how would this work if we add Jquery/Ajax?
You would call your PHP once, the result generated HAS to contain the Jquery/Ajax code in the head tag. (you need the jquery JS file added as a script tag). When the user decides to do a sort you don't call the entire PHP script again. You should call a second PHP script, dedicated to only return the HTML part you want to refresh. Here's a example:
$.post("returnhtml.php", {var1: var1, var2: var2}, function(data){
// the var data contains everything "returnhtml.php" echoed
$("#div_we_want_to_refresh_id").html(data);
});
Hope this helped.

Related

Pagination query not working

I have a problem in my pagination. My scenario is I'll just the filters then click search. Then the result will show the output. But when I click on page 2 it returns to the query for all not the query that is filtered.
Controller
<?php //CONTROLLER
function maintenance(){
if($this->session->userdata('l#ginUs3r')) {
$session_data = $this -> session -> userdata('l#ginUs3r');
$data['u_id'] = $session_data['u_id'];
$data['u_uname'] = $session_data['u_uname'];
$data['u_pword'] = $session_data['u_pword'];
$data['u_role'] = $session_data['u_role'];
$data['is_active'] = $session_data['is_active'];
$config = array();
$config["base_url"] = base_url() . "personnel/maintenance/";
$config["total_rows"] = $this -> Main -> count_prf();
$config["per_page"] = 10;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data['prf'] = $this -> Main -> select_prf($config['per_page'], $page);
$data["prf_links"] = $this->pagination->create_links();
if(isset($_POST['search']))
{
if($_POST['division'] != "" && $_POST['group'] != "" && $_POST['department'] != "" && $_POST['position'] != "" )
{
if($_POST['date_rec'] != "" && $_POST['date_upto'] != "")
{
$where = "prf_div = '".$_POST['division']."' AND prf_group = '".$_POST['group']."' AND prf_status = '".$_POST['prf_status']."' AND prf_dept = '".$_POST['department']."' AND prf_position = '".$_POST['position']."' AND prf_status = '".$_POST['prf_status']."' AND prf_daterec BETWEEN '".$_POST['date_rec']."' AND '".$_POST['date_upto']."' ";
}
else
{
$where = "prf_div = '".$_POST['division']."' AND prf_group = '".$_POST['group']."' AND prf_status = '".$_POST['prf_status']."' AND prf_dept = '".$_POST['department']."' AND prf_position = '".$_POST['position']."' AND prf_status = '".$_POST['prf_status']."' ";
}
}
//NEXT IF FOR SEARCH
if($_POST['division'] == "" && $_POST['group'] == "" && $_POST['department'] == "" && $_POST['position'] == "" )
{
if($_POST['date_rec'] != "" && $_POST['date_upto'] != "")
{
$where = "prf_status = '".$_POST['prf_status']."' AND prf_daterec BETWEEN '".$_POST['date_rec']."' AND '".$_POST['date_upto']."' ";
}
else
{
$where = "prf_status = '".$_POST['prf_status']."' ";
}
}
$_SESSION['page_list'] = $where;
$config = array();
$config["base_url"] = base_url() . "personnel/maintenance/";
$config["total_rows"] = $this -> Main -> count_prf_where($_SESSION['page_list']);
$config["per_page"] = 10;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["prf"] = $this -> Main -> fetch_prf($_SESSION['page_list'], $config["per_page"], $page);
$data["prf_links"] = $this->pagination->create_links();
}
$this -> load -> view('templates/header', $data);
$this -> load -> view('pages/maintenance', $data);
$this -> load -> view('templates/footer');
} else {
$this -> load -> view('templates/lheader');
$this -> load -> view('pages/login');
$this -> load -> view('templates/footer');
}
}
?>
View
<button type="button" class="btn btn-success btn-search-cust1 pull-right" onclick="window.location.href='<?=base_url()?>personnel/add_prf'"><span class="icon-file"></span></button>
<button type="button" name="back" class="btn btn-success btn-search-cust pull-right" onclick="window.location.href='<?=base_url()?>personnel/home'"> <span class="icon-home"></span> </button>
<?php $this -> load -> view('templates/maintenance-navbar'); ?>
<article class="grid_12 panel">
<div class=""><span class=""> </span><label class="listnames">Filter</label></div>
<div class="tab-field">
<form method="POST" action="<?=base_url()?>personnel/maintenance">
<label class="listnames">Division</label>
<select class="cust_fields" name="division">
<option value="">--Select--</option>
<?php if(!$ddl2){ ?>
<option value=""><?="NO DATA"?></option>
<?php } else { ?>
<?php foreach($ddl2 as $row){ ?>
<option value="<?=$row['d_id']?>"><?=$row['d_divname']?></option>
<?php } ?>
<?php } ?>
</select>
<label class="listnames">Group</label>
<select class="cust_fields" name="group">
<option value="">--Select--</option>
<?php if(!$ddl2){ ?>
<option value=""><?="NO DATA"?></option>
<?php } else { ?>
<?php foreach($ddl1 as $row){ ?>
<option value="<?=$row['g_id']?>"><?=$row['g_name']?></option>
<?php } ?>
<?php } ?>
</select>
<label class="listnames">PRF Status<!--FORMERLY ROLE --></label>
<select name="prf_status" id="roles" class="cust_fields">
<option value="Outstanding">Outstanding</option>
<option value ="Served">Served</option>
</select>
<br>
<label class="listnames">Department</label>
<select name="department" id="roles" class="cust_fields">
<option value="">--Select--</option>
<?php if(!$ddl3){ ?>
<option value=""><?="NO DATA"?></option>
<?php } else { ?>
<?php foreach($ddl3 as $row){ ?>
<option value="<?=$row['dept_id']?>"><?=$row['dept_name']?></option>
<?php } ?>
<?php } ?>
</select>
<label class="listnames">Position</label>
<select name="position" id="roles" class="cust_fields">
<option value="">--Select--</option>
<?php if(!$ddl5){ ?>
<option value=""><?="NO DATA"?></option>
<?php } else { ?>
<?php foreach($ddl5 as $row){ ?>
<option value="<?=$row['r_id']?>"><?=$row['r_rank']?></option>
<?php } ?>
<?php } ?>
</select>
<label class="listnames">Date Received</label>
<input type="date" name="date_rec" placeholder="mm/dd/yyyy" id="fromdate" autocomplete="off" style="height: 24px; width: 128px;">
<label class="listnames" style = "width: 15px;">To</label>
<input type="date" name="date_upto" placeholder="mm/dd/yyyy" id="fromdate" autocomplete="off" style="height: 24px; width: 128px;">
<button type="submit" name="search" class="btn btn-success cust_search"><span class="icon-search2"></span></button>
<!-- <input type="submit" name="search" value="Search"> -->
</form>
</div>
</article>
<table class="table">
<tr class="thead1" style = "background: #D6C9A4;">
<div class="row">
<div class="col-md-3">
<div class="row"><div class="col-md-3"><th >Division</th></div><div class="col-md-3"><th >Group</th></div><div class="col-md-3"><th >Department</th></div></div>
</div>
<div class="col-md-3">
<div class="row"><div class="col-md-3"><th >Position</th></div><div class="col-md-3"><th >Rank</th></div><div class="col-md-3"><th >Ex. No. of Personnel</th></div></div>
</div>
<div class="col-md-3">
<div class="row"><div class="col-md-3"><th >No. of Req. Personnel</th></div><div class="col-md-3"><th >No. of Served Personnel</th></div><div class="col-md-3"><th >Reason for PRF</th></div></div>
</div>
<div class="col-md-3">
<div class="row"><div class="col-md-3"><th >Date Received</th></div><div class="col-md-3"><th >Date Served</th></div><div class="col-md-3"><th >Age of PRF</th></div><div class="col-md-3"><th >PRF Status</th></div></div>
</div>
</div>
</tr>
<?php
if(!$prf)
{
?>
<h3 class="tab-field">No Data</h3>
<?php
}
$x = 1;
foreach($prf as $row){
$id = $row['prf_id'];
$now = time();
$your_date = strtotime($row['prf_daterec']);
$datediff = $now - $your_date;
$age = floor($datediff/(60*60*24));
$date_rec = substr($row['prf_daterec'], 0, -13);
$date_serv = substr($row['prf_dateserv'], 0, -13);
?>
<tr id="active" onclick="window.location.href='<?=base_url()?>personnel/prf_info/<?=$id?>'">
<td><?=strtoupper($row['d_divname'])?></td>
<td><?=strtoupper($row['g_name'])?></td>
<td><?=strtoupper($row['dept_name'])?></td>
<td><?=strtoupper($row['prf_position'])?></td>
<td><?=strtoupper($row['r_rank'])?></td>
<td><?=strtoupper($row['prf_ex_no_personnel'])?></td>
<td><?=strtoupper($row['prf_no_req_personnel'])?></td>
<td>Served Personnel</td>
<td><?=strtoupper($row['prf_reason'])?></td>
<td><?=$date_rec?></td>
<td><?=$date_serv?></td>
<td><?=$age?> Days</td>
<td><?=$row['prf_status']?></td>
</tr>
<?php } ?>
</table>
<div class="pagination-align"><?=$prf_links?></div>

How can I get this to display in 1 loop

In the backend of my website, I have added more categories as follows
<strong>Tour category : </strong><br />
<select name="category_id">
<?php
foreach($category_lists as $list){
if($list['id'] == $arr_collect['category_id']){
$sel = ' selected="selected" ';
}else{
$sel = null;
}
echo '<option value="'.$list['id'].'"'.$sel.'>'.$list['name'].'</option>';
}
?>
</select>
<br clear="all" />
<strong>Tour category 2: </strong><br />
<select name="category_id2">
<option value="">Select Another</option>
<?php
foreach($category_lists as $list){
if($list['id'] == $arr_collect['category_id2']){
$sel = ' selected="selected" ';
}else{
$sel = null;
}
echo '<option value="'.$list['id'].'"'.$sel.'>'.$list['name'].'</option>';
}
?>
</select>
<br clear="all" />
<strong>Tour category 3: </strong><br />
<select name="category_id3">
<option value="">Select Another</option>
<?php
foreach($category_lists as $list){
if($list['id'] == $arr_collect['category_id3']){
$sel = ' selected="selected" ';
}else{
$sel = null;
}
echo '<option value="'.$list['id'].'"'.$sel.'>'.$list['name'].'</option>';
}
?>
</select>
In the front end, I have this
<?php
$sql_ci = "SELECT * FROM location WHERE parent_id <> 0 ORDER BY parent_id, sort ";
$result_ci = mysql_query($sql_ci);
$cities = $Manager->fetchAssoc($result_ci); //peek_array($cities);
if(!empty($cities)){
foreach($cities as $city){
$i = 1; // for active category
$sql_count = "SELECT * FROM tour t, tour_category c
where t.location_id = {$city['id']} and t.type = 'P'
group by t.category_id AND t.category_id2"; //echo $sql_count.'<br />';
$result_count = mysql_query($sql_count);
$count = $Manager->fetchAssoc($result_count);
?>
<ul class="tours-accord tour-private" id="tourcity<?php echo $city['id'];?>">
<?php
$sql_ca = "SELECT * FROM tour_category ORDER BY sort";
$result_ca = mysql_query($sql_ca);
$categories = $Manager->fetchAssoc($result_ca);
if(!empty($categories)){
foreach($categories as $cat){
$sql_tour = "SELECT t.* FROM tour t
WHERE
t.touronline = 'yes'
AND t.category_id = {$cat['id']}
AND t.location_id = {$city['id']}
and t.type = 'P'
ORDER BY t.tourcode";
$result_tour = mysql_query($sql_tour);
$tours = $Manager->fetchAssoc($result_tour);
if(!empty($tours)){
?>
<li style="border-bottom:1px solid gray;">
<?php echo $city['name']; ?> - <?php echo $cat['name']; //echo ' '.$i.' ';?>
<ul>
<?php foreach($tours as $tour){
if(!empty($tour['intro_image'])){
$img = $tour['intro_image'];
}else{
$img = 'default.jpg';
}
?>
<a href="tour-details.php?code=<?php echo $tour['tourcode'];?>" class="tourname">
<li class="<?php if($i == count($count)){echo 'active';} ?>">
<img src="uploaded-items/tour-introimage/<?php echo $img; ?>" width="150" height="100" />
<span class="tourname"><?php echo $tour['tourname'].'(Code: '.$tour['tourcode'].')'; ?></span>
<p><?php echo $tour['intro']; ?></p>
<div class="home-tour-info">
<img src="images/btn-more-info-green-dark.jpg" alt="More info" class="moreinfo"/>
<div class="home-tour-price"><?php echo $tour['promotion_label']; ?></div>
</div>
</li></a>
<?php } ?>
</ul>
</li>
<?php $i++;}}} //each category ?>
The way I made it work was post the last bit 3 times and changed category_id to category_id2, category_id3. But this displayed duplicate categories.
How can I make all this just in 1 loop instead of 3?
Your backend loop is like wise as follows, as a optimising code,
<?
$numcount = 3;
for($i=0;$i<$numcount;$i++) { ?>
<strong>Tour category <?=$i+1?>: </strong><br />
<select name="category_id<?=$i+1?>">
<?php
foreach($category_lists as $list){
if($list['id'] == $arr_collect['category_id'.($i+1)]){
$sel = ' selected="selected" ';
}else{
$sel = null;
}
echo '<option value="'.$list['id'].'"'.$sel.'>'.$list['name'].'</option>';
}
?>
</select>
<br clear="all" />
<? } ?>

html select box - load image for default selected option

Today I faced with a problem about html select
here is the code :
<select id="optdon" onchange="showimage(this);" name="optdon" style="width:<?php echo $drop_width; ?>px;">
<option value="">--Select don--</option>
<?php foreach($for_don as $donname) { if(isset($donname['don_name']) && $donname['status'] == 1) { ?>
<option value="<?php echo $donname['don_id']; ?>"><?php echo $donname['don_name']?></option>
<?php } } ?>
</select>
<br /><br />
<div align="center">
<?php $i = 0; foreach($for_don as $donimage) { if(isset($donimage['don_image'])) { ?>
<div class="<?php echo $i; ?>" style="display:none;" id="<?php echo $donimage['don_id']; ?>"><img src="<?php echo $donimage['don_image']?>" /></div>
<?php $i = $i + 1;} }?>
function showimage(ids){
clearall();
if(ids.value != ''){
jQuery('#'+ids.value).show();
}
}
What is the project ? I need to select first option !!!
It is easy but when i set the first option selected it's image doesn't load in the page first load . option's image appears on changing options.
I have these two answers for selecting first option (when thre is just one option )
suggestion 1
<?php $x = 0; foreach($for_don as $donname){ if(isset($donname['don_name']) && $donname['status'] == 1) { $x = $x + 1; }} ?>
<select id="optdon" onload="showimage(this);" name="optdon" style="width:<?php echo $drop_width; ?>px;">
<option value="">--Select don--</option>
<?php $z = 0; $selected='selected' ?>
<?php foreach($for_don as $donname) { if(isset($donname['don_name']) && $donname['status'] == 1) { $z = $z + 1; ?>
<option <?php if($z==1 && $x == 1)echo $selected; ?> value="<?php echo $donname['don_id']; ?>" ><?php echo $donname['don_name']?></option>
<?php } } ?>
</select>
<br /><br />
<div align="center">
<?php $i = 0; foreach($for_don as $donimage) { if(isset($donimage['don_image'])) { ?>
<div class="<?php echo $i; ?>" style="display:none;" id="<?php echo $donimage['don_id']; ?>"><img src="<?php echo $donimage['don_image']?>" /></div>
<?php $i = $i + 1;} }?>
</div>
suggestion 2 :
<?php $x = 0; foreach($for_don as $donname){ if(isset($donname['donation_name']) && $donname['status'] == 1) { $x = $x + 1; }} ?>
<?php if($x == 1) { ?>
??????
<?php } else { ?>
thank you
Add this JavaScript:
var optdon = document.getElementById('optdon');
optdon.onchange.call(optdon);
This will trigger the onChange event handler, allowing the initial option to have its effect.

html, css and php website container div not containing

I am stuck and I am desperate. Here is the problem:
I have a div "container" that includes all the other divs.
But somehow the div ends where the first php statement is! Magic I say.
I want the container div to hold the whole page, but it reaches the first php statement (that includes the code from another file into the page) and ends. I can't find the reason.
Code.
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>{subTITLE}<?php echo TITLE; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
<meta http-equiv="description" content="{subDESCRIPTION}<?php echo DESCRIPTION; ?>">
<meta http-equiv="keywords" content="<?php echo KEYWORDS; ?>">
<link type="text/css" rel="stylesheet" href="<?php echo ABSWEBTPLPATH; ?>main.css">
</head>
<body>
<div class="superbar">
<div class="menuSearch">
<span>
<?php ShowSearchForm(); ?>
</span>
</div>
</div>
<div class="container">
<?php include(TPLPATH.'header.php'); ?>
<br/><br/><br/>
<div class="content">
<div class="contentLeft">
<div class="cLhead">
<h4>
Categories
</h4>
</div>
<ul class="categories">
<?php ShowMenu(); ?>
</ul>
</div>
<span class="contentRight">
<?php ListGoods(); ?>
</span>
</div>
<?php include(TPLPATH.'footer.php'); ?>
</div>
</body>
</html>
PHP of header.php
<div class="header-container">
<div class="wrapper">
</div>
<div id="header_top_right">
<div class="btn1">
<span class="menuPokupki"><div class="description">Мои покупки</div><div class="descriptionSub"><nobr>история покупок</nobr></div></span>
</div>
<div class="btn2">
<span>
<h5 >Мой магазин</h5>
</span>
</div>
<form action="<?php echo ABSWEBPATH; ?>ch_rt.php" method="post">
<span class="cnt">
Валюта:
<select name="rt" class="sorting">
<?php
session_start();
if(isset($_SESSION["rt"]) && !empty($_SESSION["rt"])) {
if($_SESSION["rt"] == "wmr" or $_SESSION["rt"] != "wmz" or $_SESSION["rt"] != "wme" or $_SESSION["rt"] != "wmu") {
echo "<option value=\"wmr\" selected=\"selected\">руб. (RUR)</option>\r\n"; }
else {echo "<option value=\"wmr\">руб. (RUR)</option>\r\n";}
if($_SESSION["rt"] == "wmz") {
echo "<option value=\"wmz\" selected=\"selected\">$ (USD)</option>\r\n"; }
else {echo "<option value=\"wmz\">$ (USD)</option>\r\n";}
if($_SESSION["rt"] == "wme") {
echo "<option value=\"wme\" selected=\"selected\">€ (EUR)</option>\r\n"; }
else {echo "<option value=\"wme\">€ (EUR)</option>\r\n";}
if($_SESSION["rt"] == "wmu") {
echo "<option value=\"wmu\" selected=\"selected\">грн. (UAH)</option>\r\n"; }
else {echo "<option value=\"wmu\">грн. (UAH)</option>\r\n";} }
else {
if(isset($default_rt) && !empty($default_rt)) {
if($default_rt == "wmr" or $default_rt != "wmz" or $default_rt != "wme" or $default_rt != "wmu") {
echo "<option value=\"wmr\" selected=\"selected\">руб. (RUR)</option>\r\n"; }
else {echo "<option value=\"wmr\">руб. (RUR)</option>\r\n";}
if($default_rt == "wmz") {
echo "<option value=\"wmz\" selected=\"selected\">$ (USD)</option>\r\n"; }
else {echo "<option value=\"wmz\">$ (USD)</option>\r\n";}
if($default_rt == "wme") {
echo "<option value=\"wme\" selected=\"selected\">€ (EUR)</option>\r\n"; }
else {echo "<option value=\"wme\">€ (EUR)</option>\r\n";}
if($default_rt == "wmu") {
echo "<option value=\"wmu\" selected=\"selected\">грн. (UAH)</option>\r\n"; }
else {echo "<option value=\"wmu\">грн. (UAH)</option>\r\n";} }
else {
echo "<option value=\"wmr\" selected=\"selected\">руб. (RUR)</option>
<option value=\"wmz\">$ (USD)</option>
<option value=\"wme\">€ (EUR)</option>
<option value=\"wmu\">грн. (UAH)</option>\r\n"; } }
?>
</select>
<input type="submit" value="Установить" class="button" />
</form></span></div>
</div>
<div class="subheader">
<ul class="menu">
<li class="menu1" noWrap>О магазине</li>
<li class="menu1" noWrap>Способы оплаты</li>
<li class="menu1" noWrap>Контакты</li>
<span class="filter">
<span class="sortingTitle">Сортировка:</span>
<span class="sortingSelect">
<?php GoodsSort(); ?>
</span>
</span>
</ul>
</div>
</div>
</div>
CSS has 500 lines so I uploaded it to pastebin
sorry for terrible crap code. I have to urgently edit someone else's and I already hate him
http://pastebin.com/6b5UC9mj
Too many closing divs in header.php
Take the last one out
Your span is closed outside the form. Your current structure is :
<form action="<?php echo ABSWEBPATH; ?>ch_rt.php" method="post">
<span class="cnt">
....
</form>
</span>
Must be:
<form action="<?php echo ABSWEBPATH; ?>ch_rt.php" method="post">
<span class="cnt">
....
</span>
</form>

While-loop: group two result on one div

I would add each two result one div class
<?php while ($fetch = $db->fetch($query)) { ?>
<?php echo $fetch['title']; ?>
<?php } ?>
Output should be like this
<div class="one">
<div class="two">
<article>Title</article>
<article>Title</article>
</div>
</div>
<div class="one">
<div class="two">
<article>Title</article>
<article>Title</article>
</div>
</div>>
<?php
$i=0;
while ($fetch = $db->fetch($query)) { ?>
<?php if ($i%2==0) { ?>
<div class="one">
<div class="two">
<?php } ?>
<article><?php echo $fetch['title']; ?></article>
<?php if ($i++%2==1) { ?>
</div>
</div>
<?php } ?>
<?php } ?>
//Also is a good idea to verify if the <div> tags are closed
<?php if ($i%2==1) { ?>
</div>
</div>
<?php } ?>
$count = 0;
while ($fetch = $db->fetch($query))
{
if ($count == 0)
echo '<div class="one"><div class="two">';
if ($count < 2)
echo '<article>'.$fetch['title'].'</article>';
if ($count == 2)
{
echo '</div></div>';
$count = 0;
}
$count++;
}

Categories