how to set default la page as default in css - php

I have been struggled with one sticky issue regarding to set default li.
what I try to achieve is that set a li and its related page as default one. And when users click on others links, it would load other pages.
HTML Markup:
<div id="productIntro">
<div id="leftNav">
<ul>
<li class="head">
Pictures</b>
</li>
<li class="head">
<b>Comments</b>
</li>
<li class="head">
<b>Others</b>
</li>
</ul>
</div>
<div class="main">
</div>
</div>
JS:
function show(id, page) {
$('.main').load("loadProDetail.php?id=" + id + "&page=" + page, true);
}
jQuery(document).ready(function () {
$(document).on('click', '.head', function () {
$(".head").removeClass("selected");
$(this).toggleClass("selected");
});
});
loadProDetail PHP
<?php
$id = $_GET['id'];
$sql = "select * from product where id=$id ";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
$page = $_GET['page'];
if($page == 1)
{
$html .= '<img src="./product_images/' . $row["name"] . '.jpg" width="150" border="2">';
}
if($page == 2)
{
$html .= 'No comments so far';
}
if($page == 3)
{
$html .= 'This page is under construction';
}
echo $html;
CSS:
.selected{background-color:red;}
In my case, I want to set the "Picutre" li as default page, and when user click on comments, it would load corresponding page. And also the background colour should also be changed.

Your page logic lives in your PHP, so specifying which page should display by default can be added there by modifying your if statements and making them into if...else instead:
if ($page==2) {
$html .= 'No comments so far';
} else if ($page==3) {
$html .= 'This page is under construction';
} else {
$html .= '<img src="./product_images/'.$row["name"].'.jpg" width="150" border="2">';
}
The logic implemented above goes like this: if the "Comments" or "Others" pages are requested, serve them, but if not then just serve the "Pictures" page.
Since you also want to load the "Picture" content when the page is initially loaded, you can call the show() function in your Javascript on pageload. Adding the following line to your existing jQuery(document).ready(function () { ... } should work as long as $row["id"] is properly set:
show($row["id"], 1);

Related

print the data on different pages in php using jquery

I have some data displayed on screen
php:
echo'
<script>
$(document).ready(function(){
window.print();
});
</script>';
$sql="SOME QUERY";
$result=$conn->query($sql);
if($result->num_rows>0)
{
while($row=$result->fetch_assoc())
{
echo'<div id="one">
....content....
</div>';
echo'<div id="two">
....content....
</div>';
}
}
now when the page loads the whole page displayed gets printed ( as the function suggest in jquery), but is it possible to print #one on page 1 , and #two on next page (no matter how much content is in these division)
Create a page class for your elements, and add a page-break after them using CSS, so each will be printed to a new page
#media print {
.page{page-break-after: always;}
}
And change the php code to:
while($row=$result->fetch_assoc())
{
echo'<div id="one" class="page">
....content....
</div>';
echo'<div id="two" class="page">
....content....
</div>';
}

Jquery load() get a variable from another page

On my main page I have a content box where I load the content using Jquery's load() from another page. All is working fine and it's quick and nice.
Next thing I want to do is to add a small filtering feature to it. The variable is sent to the main page (snappage.php) as a GET variable. However the php for the sql query is in another page (i.e all-snaps.php). Let me show you my code:
snappage.php
<?php
require "database/database.php";
session_start();
if($_GET['country']) {
$country = $_GET['country'];
}
?>
<section class="main-snap-page-wrapper">
<nav class="all-snaps-countries">
<h4>Filter by country</h4>
<ul>
<?php
//GET COUNTRY FLAGS
$flagsql = mysql_query("SELECT * FROM countries");
while($getflag = mysql_fetch_array($flagsql)) {
$countryname = $getflag['countryname'];
$flag = $getflag['countryflag']; ?>
<li>
<a href="snappage.php?country=<?php echo $countryname?>">
<img src="<?php echo $flag?>" alt="">
<h5><?php echo $countryname?></h5>
</a>
</li>
<?php } ?>
</ul>
</nav>
<div class="all-snaps-page">
<nav class="main-page-tabs-wrapper">
<ul class="main-page-tabs" id="<?php echo $country?>">
<li class="active-tab">All</li>
<li>Female</li>
<li>Male</li>
</ul>
<div class="main-snaps-content"></div>
</nav>
</div>
</section>
<script type="text/javascript">
$('.main-snaps-content').load('all-snaps.php');
$('.main-page-tabs li').on('click', function () {
$('.main-page-tabs li').removeClass('active-tab');
$(this).addClass('active-tab');
var page = $(this).find('a').attr('href');
$('.main-snaps-content').load(page + '.php');
return false;
});
</script>
Next is the page which I load into .main-snaps-content from i.e all-snaps.php:
all-snaps.php
<?php
require "database/database.php";
session_start();
if($_GET['country']) {
$country = $_GET['country'];
$newsql = mysql_query("SELECT * FROM users JOIN fashionsnaps ON users.id = fashionsnaps.userid WHERE country = '$country' ORDER BY snapid ASC");
}
else {
$newsql = mysql_query("SELECT * FROM fashionsnaps ORDER BY snapid ASC");
}
?>
<ul class="snaps-display">
<?php
//GET SNAPS BY ID
while ($getnew = mysql_fetch_array($newsql)) {
$newsnappics = $getnew['snappic'];
$newsnapid = $getnew['snapid'];
?>
<li>
<a href="snap.php?id=<?php echo $newsnapid?>">
<img src="<?php echo $newsnappics?>" alt="">
</a>
</li>
<?php } ?>
</ul>
So what I want to achieve here is to load the filtered content from all-snaps.php into the .main-snaps-content which is on snappage.php.
Where should I send this $country variable? On which page should I retrieve it?
You can do this pretty easily with jQuery load.
In snappage.php:
$(".main-snaps-content").load("all-snaps.php?" + $.param({country: "<?php echo htmlentities($country); ?>"}));
Update:
You'll need better handling of the PHP GET/$country var. You can initialize it at the top of snappage.php like this:
$country = (isset($_GET['country'])) ? $_GET['country'] : '';
Then, your JS will need a conditional:
var country = "<?php echo htmlentities($country); ?>";
if (country) {
$(".main-snaps-content").load("all-snaps.php?" + $.param({country: country}));
} else {
$(".main-snaps-content").load("all-snaps.php");
}

How to "reload" a div's content?

In my page there is a div element containing a MetroUI listview :
...
<div id="listSalles" class="listview">
<?php
$ret = ReservationSalle::lireParCritere([]); // database SELECT query
if ($ret->count() > 0) {
$html = '';
foreach ( $ret as $key => $val ) {
$html .= '<div class="list" id="salle_'.$ret[$key]->salle_code.'_'.$ret[$key]->flag_reserver.'">
<span class="mif-bookmarks list-icon"></span>
<span class="list-title">'.$ret[$key]->salle_lib.'</span>
<span class="place-right"><button id="reservS_'.$ret[$key]->salle_code.'" class="button default">Réserver</button></span>
<br/>
<span class="sub-title">'.$ret[$key]->reserver.'</span>
</div>';
}
echo $html;
}
else {
echo '<br/><div class="sub-header">Aucun enregistrement</div>';
}
?>
</div>
...
As you can see there is a database SELECT query which populates the listview. I want to "reload" this listview's content when a button , outside of the listview , is pressed. How to do that ?
Use javascript to do so.
In your PHP script put your current code:
$ret = ReservationSalle::lireParCritere([]); // database SELECT query
if ($ret->count() > 0) {
$html = '';
foreach ($ret as $key => $val) {
$html .= '<div class="list" id="salle_' . $ret[$key]->salle_code . '_' . $ret[$key]->flag_reserver . '">
<span class="mif-bookmarks list-icon"></span>
<span class="list-title">' . $ret[$key]->salle_lib . '</span>
<span class="place-right"><button id="reservS_' . $ret[$key]->salle_code . '" class="button default">Réserver</button></span>
<br/>
<span class="sub-title">' . $ret[$key]->reserver . '</span>
</div>';
}
return $html;
} else {
return '<br/><div class="sub-header">Aucun enregistrement</div>';
}
Lets call it data.php.
Then in your page load jQuery (if you havent yet, because it's easier) and add the following JS:
$.ajax({
type: 'GET',
url: 'data.php'
}).done(function (result) {
$("#listSalles").html(data); //Here you replace the current content with the update
}, "html");
And then wrap this JS in $(document).ready(function() {}) and inside a click listener $(btn).click(function(){ });
I suggest using AJAX.
Make a view with the relevant php code for the task, like this:
<?php
$ret = ReservationSalle::lireParCritere([]); // database SELECT query
if ($ret->count() > 0) {
$html = '';
foreach ( $ret as $key => $val ) {
$html .= '<div class="list" id="salle_'.$ret[$key]->salle_code.'_'.$ret[$key]->flag_reserver.'">
<span class="mif-bookmarks list-icon"></span>
<span class="list-title">'.$ret[$key]->salle_lib.'</span>
<span class="place-right"><button id="reservS_'.$ret[$key]->salle_code.'" class="button default">Réserver</button></span>
<br/>
<span class="sub-title">'.$ret[$key]->reserver.'</span>
</div>';
}
echo $html;
}
else {
echo '<br/><div class="sub-header">Aucun enregistrement</div>';
}
?>
Let's call it list-view.php.
Then in your page, add the following jquery:
$("#listSalles").load("list-view.php");
That would load the view inside that div.
If you want to reload that content when you press a button you could add a click handler for that button:
$("#id_of_button").click(function(){ $("#listSalles").load("list-view.php"); });

Jquery delete function

I'm trying to make Jquery delete function, here is the code, that I wrote -
$("a.delete").click(function(){
var target = $(this).attr("id");
var c = confirm('Do you really want to delete this category?');
if(c==true) {
$(target+"ul").delay(3000).fadeOut(200);
}
else {
return false;
}
});
Okay now to problem, when I press, button a with class delete, it correctly, shows up the dialog (and the id is correct), but after I press yes, it will delete it with php, without jquery fadeOut effect.
My php code -
public function deleteCategory() {
if(isset($_GET['action']) && $_GET['action'] == 'delete') {
$id = (int)$_GET['id'];
$sql = "DELETE FROM `categories` WHERE `id` = '".$id."'";
mysql_query($sql);
}
}
Not sure where is the problem but I think it's inside $(target+"ul"), since I'm not sure, if it will add the target value and ul together.
In additional, how can I prevent the delete, if I press no? Currently, if I press no, it will still delete the category.
If you need any other information - ask.
Full code -
public function showCategories() {
if(isset($_SESSION['logged_in']) && isset($_SESSION['user_level']) && $_SESSION['user_level'] == 'admin') {
$sql = "SELECT * FROM `categories`";
$q = mysql_query($sql);
if(mysql_num_rows($q) > 0) {
?>
<div class="recentorders" style="display: none;" id="categoryBox">
<h5 class="colr">Categories</h5>
<div class="account_table" style="width: 688px;">
<ul class="headtable" style="width: 680px;">
<li class="order">ID</li>
<li class="action" style="width: 430px;">Category Name</li>
<li class="action nobordr">Options</li>
</ul>
<?php
while($row = mysql_fetch_array($q)) {
?>
<ul class="contable" style="width: 680px;" id="cat<?php echo $row['id']; ?>ul">
<li class="order"><?php echo $row['id']; ?></li>
<li class="action" style="width: 430px;"><?php echo $row['name']; ?></li>
<li class="action nobordr">Edit<a class="delete" id="cat<?php echo $row['id']; ?>" href="?action=delete&id=<?php echo $row['id']; ?>#">Delete</a></li>
</ul>
<?php
}
?>
</div>
</div>
<?php
}
else {
// No categories created, please create one first.
}
}
else {
header('location: account.php');
}
}
public function deleteCategory() {
if(isset($_GET['action']) && $_GET['action'] == 'delete') {
$id = (int)$_GET['id'];
$sql = "DELETE FROM `categories` WHERE `id` = '".$id."'";
mysql_query($sql);
}
}
If you're searching by ID you need to prefix your selector with a "#":
100% working sample on jsFiddle:
http://jsfiddle.net/E7QfY/
Change
$(target+"ul").delay(3000).fadeOut(200);
to
$('#' + target+"ul").delay(3000).fadeOut(200);
OR
$(this).closest('ul').delay(3000).fadeOut(200);
I would also modify the code to this:
$("a.delete").click(function(event){
if(confirm('Do you really want to delete this category?') == true){
$(this).closest('ul').delay(3000).fadeOut(200);
window.location.href = $(this).attr('href'); //OR use AJAX and do an event.preventDefault();
}
else
event.preventDefault();
});
It's because you are using an anchor tag that is causing a postback to the server which makes your page refresh before you see the animation. Make sure your anchor tag has an href defined as "#" to stop the postback from happening.
You should use something else than A tag e.g. span or div. And then after clicking this element make your fadeout and after it redirect to the PHP script that deletes your object:
$("SPAN,DIV.delete").click(function(){
var target = $(this).attr("id");
var c = confirm('Do you really want to delete this category?');
if(c==true) {
$(target+"ul").delay(3000).fadeOut(200);
window.location.href = URL TO PHP FILE
}
else {
return false;
}
});

utilize PHP variable in javascript

I just asked another question here: global variable and reduce database accesses in PHP+MySQL
I am using PHP+MySQL. The page accesses to the database and retrieve all the item data, and list them. I was planning to open a new page, but now I want to show a pop div using javascript instead. But I have no idea how to utilize the variables of PHP in the new div. Here is the code:
<html>
</head>
<script type="text/javascript">
function showDiv() {
document.getElementById('infoDiv').style.visibility='visible';
}
function closeDiv() {
document.getElementById('infoDiv').style.visibility='hidden';
}
</script>
</head>
<body>
<ul>
<?php foreach ($iteminfos as $iteminfo): ?>
<li><?php echo($iteminfo['c1']); ?></li>
<?php endforeach;?>
</ul>
<div id="infoDiv" style="visibility: hidden;">
<h1><?php echo($c1) ?></h1>
<p><?php echo($c2) ?></p>
<p>Return</p>
</div>
</body>
</html>
"iteminfos" is the results from database, each $iteminfo has two value $c1 and $c2. In "infoDiv", I want to show the details of the selected item. How to do that?
Thanks for the help!
A further question: if I want to use, for example, $c1 as text, $c2 as img scr, $c1 also as img alt; or $c2 as a href scr, how to do that?
Try this:
<?php foreach ($iteminfos as $iteminfo): ?>
<li>
<a href="javascript:showDiv(<?php echo(json_encode($iteminfo)) ?>)">
<?php echo($iteminfo['c1']); ?>
</a>
</li>
<?php endforeach;?>
Also, modify showDiv to take your row data:
function showDiv(row) {
document.getElementById('infoDiv').style.visibility='visible';
document.getElementById('infoDiv').innerHTML = row['c1'];
}
Basically, you have to consider that the javascript runs in the browser long after the PHP scripts execution ended. Therefore, you have to embed all the data your javascript might need into the website or fetch it at runtime (which would make things slower and more complicated in this case).
Do you want a single info area with multiple items listed on the page and when you click an item the info area is replaced with the new content??? or you want a new info area for each item??
I see something along the lines of the first approach, so I will tackle the latter.
<?php
//do some php magic and get your results
$sql = 'SELECT title, c1, c2 FROM items';
$res = mysql_query($sql);
$html = '';
while($row = mysql_fetch_assoc($res)) {
$html .= '<li><a class="toggleMe" href="#">' . $row['title'] . '</a><ul>';
$html .= '<li>' . $row['c1'] . '</li><li>' . $row['c2'] . '</li></ul>';
$html .= '</li>'; //i like it when line lengths match up with eachother
}
?>
<html>
</head>
<script type="text/javascript">
window.onload = function(){
var els = document.getElementsByClassName("toggleMe");
for(var i = 0, l = els.length; i < l; i++) {
els[i].onclick = function() {
if(this.style.display != 'none') {
this.style.display = 'block';
} else {
this.style.display = 'none';
}
}
}
}
</script>
</head>
<body>
<ul>
<?php
echo $html;
?>
</ul>
</body>
</html>

Categories