I'm trying to learn how to use Sessions to maintain state between pages.
I think my problem is that I dont know how to escape properly. When I load the page im not getting the SID at the end of the url, instead I'm getting this:
mytestsite.com/login.php?<?php echo SID;?>
I have session_start() at the top of every page. And the session initialy works as I see the login name appear on the page and the Register and Login links, but then when I click to another page the session is not maintained.
The main site nav is dynamically createdusing an array($nav) and foreach loop. Here too I'm not sure how to echo the url and php SID constant. I don't think my syntax is correct.
//LOGIN BAR
echo "<nav id='statusBar'>";
echo "<ul>";
if(isset($_SESSION['userIn']) && $_SESSION['userIn'] != ''){
echo "<li id='userLo'>Log Out</li>";
echo "<li id='userLi'>User Logged In: " . $_SESSION['userIn'] . "</li>";
} else{
echo '<li>Register</li>';
echo '<li>Login</li>';
}
echo "</ul>";
echo "</nav>";
//START MAIN MENU
echo "<nav id='nav'> ";
$nav = array();
$nav['index.php?<?php echo SID;?>'] = 'Home';
$nav['public1.php?<?php echo SID;?>'] = 'Public 1';
$nav['public2.php?<?php echo SID;?>'] = 'Public 2';
$nav['members.php?<?php echo SID;?>'] = 'Members';
//CREATE UNORDERED LIST
echo '<ul>';
foreach ($nav as $key => $navValues) {
echo "<li>";
echo "<a href='$key'>$navValues</a>";
echo "</li>";
}
echo '</ul>';
echo "</nav>";
Below everything within echo '..'; is interpreted as html.
echo '<li>Login</li>';
If you want to use variables inside the string you have to concatenate them like so:
echo 'Login' ;
For concatenating php code take a look at:
http://php.net/manual/en/language.operators.string.php
and more about sessions:
http://php.net/manual/en/session.constants.php
Related
I'd like to add an active class with PHP instead of using javascript.
But I probably broke the code somewhere.
I'm using bootstrap.js version 3 and I'm building my tabs using PHP because I need to use my tabs on the control panel in my app.
So essentially I didn't add any javascript or jquery so far, I'm just using the native functions for tabs in bootstrap.js (and jquery):
<?php foreach ($database as $item): ?>
<?php
if (isset($item['associations'])) {
foreach ($item['associations'] as $value) {
echo "<div class='modal' id='des_" . $value['des_id'] . "'>";
echo "<div class='modal-sandbox'></div>";
echo "<div class='modal-box'>";
echo "<div class='modal-body'>";
echo "<div class='close-modal'>✖</div>";
echo "<h3>Credit and values</h3>";
echo "<h5>" . $value["designation_name"] . " - Credit: " . $value["designation_credits"] . "</h5>";
echo "<ul class='nav nav-pills'>";
if (isset($value['credits']) && !is_string($value['credits'])) {
foreach ($value['credits'] as $credits) {
$first_element = reset($value["credits"]);
if (isset($value['credits'])) {
if ($first_element[4] == $credits[4]) {
echo "<li class='active'><a href='tab_" . $credits[4] . "' data-toggle='tab'>Identifier: " . $credits[0] . "</a></li>";
} else {
echo "<li><a href='tab_" . $credits[4] . "' data-toggle='tab'>Identifier: " . $credits[0] . "</a></li>";
}
}
}
}
echo "</ul>";
echo "<div class='tab-content clearfix'>";
if (isset($value['credits']) && !is_string($value['credits'])) {
foreach ($value['credits'] as $credits) {
if (isset($value['credits'])) {
if ($first_element[4] == $credits[4]) {
echo "<div class='tab-pane active' id='tab_" . $credits[4] . "'>";
} else {
echo "<div class='tab-pane' id='tab_" . $credits[4] . "'>";
}
echo "<div class='single-credit'>";
?>
<?=form_open();?>
<?php
echo "<input type='hidden' name='id' value='" . $credits[4] . "'>";
echo "<div>Identifier: <span>" . $credits[0] . "</span></div>";
echo "<input name='edit_credits' type='submit' value='Edit' class='submit'>";
echo "</div>";
echo "</div>";
echo "</div>";
?>
<?=form_close()?>
<?php
}
}
}
echo "</div>";
echo "</div>";
echo "</div>";
echo "</div>";
}
}
?>
<?php endforeach;?>
To explain my code I will start from the list:
Since it's a really complex array, I'm taking the first element with the reset function to add the active class only on that very element.
Then I added the href element to point to the id of the matching div later on, and then I close my list.
Later on, I create another loop and I use the id to match both the list and the div and I used the trick that I used before to add the class active only on my first element of my array.
Everything works except for the fact that, if I click another element of the list, the matching div won't work, the very first element of the array will keep the class active, so I can't switch beteween element.
I need to spot the error.
UPDATE:
The problem that I have with jquery, in this case, is that if I use a toogleClass for example like that:
$(".nav-pills li").click(function(e) {
e.preventDefault();
tab = $(this).find('a').attr('href');
$("#" + tab).toggleClass('active');
});
It won't be able to toggle properly the class on the first element, that one to which I added the class active with php.
I think you miss to change the URL when click it, in the backend (PHP) you need to know where the user is, so, you check the "parameter" and mark active or not the current tab.
For example:
printf("<div class='tab-pane %s' id='...'>", $credits[4] == $_GET['tab'] ? 'active':'');
I created a simple php/mysql pagination and I'd like to get current page to set the class active . The answers I found around here do not necessarily represents the kind of pagination I have created which is very basic.
pagination
<?php
//pagination
$perpage=403;
if(isset($_GET['page'])){
$page=$_GET['page'];
}
else{
$page=1;
}
$offset=($page-1)*$perpage;
$sqlu="SELECT * FROM ve_users u
ORDER BY IsOnline DESC";
$resultu=mysqli_query($db,$sqlu);
$total_rows=mysqli_num_rows($resultu);
$total_pages=ceil($total_rows/$perpage);
echo"<div class='paginations'>";
echo "<a href='users.php?page=1'>«</a>";
for($i=1;$i<=$total_pages;$i++){
echo "<a class='' href='users.php?page=$i'>$i</a>";
}
echo "<a href='users.php?page=$total_pages'>»</a>";
echo"</div>";
?>
$page is where you capture the current page, so you have to change the pagination links' code block in the following way,
// your code
echo "<a href='users.php?page=1'>«</a>";
for($i=1;$i<=$total_pages;$i++){
if($i == $page){
echo "<a class='active' href='users.php?page=$i'>$i</a>";
}else{
echo "<a href='users.php?page=$i'>$i</a>";
}
}
echo "<a href='users.php?page=$total_pages'>»</a>";
// your code
how can i pass the id as the name.I used this code.
<?php
$i = 1;
foreach ($view as $row)
{
echo "<tr>";
echo "<td>".$i."</td>";
echo "<a href="<?php echo base_url();?>admin/play/songs/<?php echo $row->audio"><td>". $row->audio . "</td>";
$i++;
echo "</tr>";
}
?>
I want to play the song when user press the name of the song.but i am not able to pass the name of the song to my controller "play".so please help and this is my controller where i want to pass the data.
class Play extends CI_Controller {
public function songs($name){
$data['name'] = $name;
$this->load->view('admin/play',$data);
}
You should grab id instead of name it will make your job easier.
<?php
$i = 1;
foreach ($view as $row)
{
echo "<tr>";
echo "<td>".$i."</td>";
echo "<a href="<?php echo base_url();?>admin/play/songs/<?php echo $row->id"><td>". $row->audio . "</td>";
$i++;
echo "</tr>";
}
?>
First of all you should know that you can pass the url you want after base_url as a parament in base_url like
base_url('admin/play/songs/');
Secondly you can pass anything to controller via this method
base_url('admin/play/songs/{$row->audio}');
Just excited to know if it works
I need to do something like
$var = "Hello World ..the value is <?php echo 'XYZ' ?>";
Any help will be highly useful. I am stuck in this from the very long time but not able to acheive this. please help
Below is my PHP code that I need to assigned again to php variable (this variable will be passed to return of my ajax call)
<div class="photo-post">
<h4 class="h4-1">Some data...</h4>
<?php
$i=0;
$unique_seq="888094499";
$pic_counter=1;
foreach (glob("target/*".$unique_seq."*") as $filename)
{
$i++;
}
$total_pics=$i;
echo "<div id='post-photo-slider'>";
if($i>1)
{
echo "<a href='#' class='control_next_photo'>></a>";
echo "<a href='#' class='control_prev_photo'><</a>";
}
echo "<ul>";
foreach (glob("target/*".$unique_seq."*") as $filename)
{
echo "<li>";
echo "<div class='photos-slide-container'>";
echo "<img src='".$filename."' width='540px' height='225px'>";
echo "<span class='count-span-photos'>".$pic_counter." of ".$total_pics."</span>";
echo "</div>";
echo "</li>";
$pic_counter++;
}
if ($i==1)
{
echo "<li></li>";
}
echo "</ul>";
echo "</div><!-- post-photo-slider END -->";
echo "<hr class='hr4'>";
?>
</div><!-- photo end -->
you cannot do it in this way
$var = "Hello World ..the value is <?php echo 'XYZ' ?>";
when you return this string to your javascript php code cannot be execute on client side!!!!
why do not return do this simple way?
$var = "Hello World ..the value is " . "XYZ";
I am going to write following code for my blog. But problem, I am facing with:
"echo 'a href=".$query2['url']."</a>';" I want to add .$query2['url']. in echo and also want to add .html at the end of url like
"a href=".$query2['url'].html.".
If you have any idea please tell me as soon as possible thanks. Basically I am fetching this data from database .
<?php
$start=0;
$limit=5;
if(isset($_GET['id']))
{
$id=$_GET['id'];
$start=($id-1)*$limit;
}
$query=mysql_query("select * from tbl_services LIMIT $start, $limit");
echo "<div class='entry'>";
while($query2=mysql_fetch_array($query))
{
echo '<h2>' .$query2['name'].'</h2>';
echo "<p>".$query2['Contents']."</p>";
echo "<div class='meta'><i class='fa fa-clock-o'></i>".$query2['date']. "<b><i class='fa fa-user'></i></b> Written by <strong>Arslan Ali</strong> <b><i class='fa fa-comment-o'></i></b>" ;
echo "</div>";
}
echo "</div>";
$rows=mysql_num_rows(mysql_query("select * from tbl_services"));
$total=ceil($rows/$limit);
echo "<ul class='pagination'>";
if($id>1)
{
echo "<li><a href='?id=".($id-1)."' class='disabled'><<</a></li>";
}
for($i=1;$i<=$total;$i++)
{
if($i==$id) { echo "<li class='current'>".$i."</li>"; }
else { echo "<li><a href='?id=".$i."'>".$i."</a></li>"; }
}
if($id!=$total)
{
echo "<li><a href='?id=".($id+1)."' class='disabled'>>></a></li>";
}
echo "</ul>";
?>
(Updated)
Replace this line
echo '<h2>' .$query2['name'].'</h2>';
with
echo "<a href='".$query2['url'].".html'>".$query2['name']."</a>";
There is still one problem with the solution made by Syntax Error. The href is in single quotes. This is not the proper Syntax. It is easily remedied by reversing the use of the single and double quotes as I have shown here.
echo '' . $query2['name']. '';