Dynamic navigation with php - php

Hi I am building a site directory and I'm having a little trouble understanding how to use the $_GET['page'] function
I understand you can use this to navigate across sites which are dynamically made, in fact I have a site which uses it currently but I did not create that set of code and don't understand the logic behind it..
I know how I will handle the navigation - the homepage will contain sites with a link to both their website and to a listing on my site. The listing url will contain the category and the ID of the site.
I want it to look like domain/category/siteid or like domain/index.php?cat=2&id=58
I understand I will probably need to use htaccess to change the url to look like the first example..
So anyway, the url will contain both the site ID and category which I want to then use to associate with the dynamically loaded page, with the relevant category and site listing.
This seems to be the standard way of loading pages in PHP but I've been unable to find a good guide on how to do it, I will also need to read the data from the url to make a database call so if anyone could explain how I can read the url to get the listing id and category it would be greatly appreciated!
Thanks a lot
Luke

You answered your question more or less yourself. The parameters from the URL are available in PHP in the $_GET array. For your example the category would be $_GET['cat'] (would return 2) and the ID would be $_GET['id'] (would return 58).
Take care of SQL-Injections. Also see other global variables

<ul>
<li>HOME</li>
<?
$first_query = "SELECT * FROM navigation_menu WHERE `status` ='active'";
$first_menu = $db->select($first_query);
for($a = 0 ; $a < count($first_menu) ; $a++)
{?>
<li> <?=$first_menu[$a]['navigation']?>
<?$second_query = "SELECT * FROM sub_menu WHERE nav_menu_id=".$first_menu[$a]['id']." AND `status` ='active'";
$second_menu = $db->select($second_query);
if(count($second_menu) > 0)
{?>
<ul>
<?for($b = 0 ; $b < count($second_menu) ; $b++)
{
if($second_menu[$b]['sub_nav_menu'] != '')
{?>
<li> <?=$second_menu[$b]['sub_nav_menu']?>
<?$third_query = "SELECT * FROM sub_child_menu WHERE nav_menu_id=".$first_menu[$a]['id']." AND sub_nav_id=".$second_menu[$b]['id']." AND `status` ='active'";
$third_menu = $db->select($third_query);
if(count($third_menu) > 0)
{?>
<ul>
<?
for($c = 0 ; $c < count($third_menu) ; $c++)
{?>
<li>
<a href="general.php?id=<?=$first_menu[$a]['id']?>&sid=<?=$second_menu[$b]['id']?>&cid=<?=$third_menu[$c]['id']?>&general=c">
<?=$third_menu[$c]['child_menu']?>
</a>
</li>
<?}?>
</ul>
<?}?>
</li>
<?}
}?>
</ul>
<?}?>
</li>
<?}?>
</ul>

Related

pagination php is not working, getting stuck on the first one, even tho if i echo the variables that store the page data are correct

I am trying to add pagination to my CRUD PHP app but it's not working properly.
Trying to limit the data from my SQL database to 5 per page, but it's getting stuck to the first page. If I press the second one, stays on the first page data.
$page = (isset($_GET['page']) ? $_GET['page'] : 1);
$perPage = (isset($_GET['per-page']) && ($_GET['per-page']) <= 50 ? $_GET['per-page'] : 5);
$start = ($page > 1) ? ($page * $perPage) - $perPage : 0;
$sql = "select * from movies limit ".$start." , ".$perPage." ";
$total = $db->query("select * from movies")->num_rows;
$pages = ceil($total / $perPage);
And here I am looping through the pages in the HTML code below the PHP one.
<center>
<ul class="pagination">
<?php for($i = 1; $i <= $pages; $i++): ?>
<li>
<?php echo $i; ?>
</li>
<?php endfor; ?>
</ul>
</center>
I am using bootstrap class for pagination which is added with a script in the head. Any idea why I stay at the first page even when I press the second one?
Here is a photo from my web project, in the url bar u can see i am at the second page, but still showing the data from first. In my database i have 10 elements, so it should be 5 and 5.
Looks like you are using the wrong variable name in your link. It should be:
<a href="?page=<?php echo $i; ?>">
(note, page not pages. Also, remove the spaces which get encoded as %20 in the url)
Be vary careful of putting user-submitted data into your query though, google "SQL injection" to find out more.

Making A List Using PHP And NestedSortable

I'm attempting to make my own CMS and one thing I wanted to do was make it so I could reorder and change the navigation elements in the CMS. I found a plugin called NestedSortable that let me do this. I used it and had a lot of trouble with it, so I got my expert PHP friend to help me do it. She sat down, struggled for a bit and then got it to work.
Well, the idiot I was, when I got home later that day I sat down, took my local copy, added a something to it and uploaded it to the server... not realizing I had forgotten to download the newer version! So poof, all my friend's work is gone and I'm stuck with a navigation adjustment that doesn't work.
I lost the index.php (that displays it on the page) file but I saved the nav_adjust.php (that inputs it into the database). All I want is to be able to re-order the navigation and send it to the database to be displayed correctly on refresh. The information is send to nav_adjust.php when the submit button is pressed.
The only limitation is that the navigation can only have 3 levels to it (primary, child and grandchild). My navigation looks like when they're all primary. I have alerted back the echo for all the images so you can see what nav_adust.php is doing:
(Please note the array values.)
This is as if there was one child on a parent:
This is as if there was two grandchildren on a child:
And this is mixed:
My index.php file, which doesn't work anymore, looks like this:
<form method="post" action="nav_adjust.php"> <!--This form posts to nav_adjust.php-->
<ol id="pageList">
<?php
$nav = mysql_query("SELECT * FROM `".$name['Website']);
$num = 0;
while( $col_nav = mysql_fetch_assoc($nav) ){
if ($col_nav['orderId'] != 0) {
echo "<li id='list_".$col_nav['id']."'>
<div>Example Title</div>";
} else {
echo "<ol>
<li id='list_".$col_nav['id']."'>
<div><h1>Example Title</div>";
</li>
</ol>\n";
}
echo "</li>\n";
++$num;
}
?>
</ol>
<button type='submit'>Update</button>
</form>
<script>
$(document).ready(function(){
$('#pageList').nestedSortable({
/*option that don't matter*/
});
$("button").on('click', function(event){
serialized = $('ol#pageList').nestedSortable('serialize');
$.post( "nav_adjust.php", serialized, function( data ) {
alert( data );
});
return false;
});
});
</script>
Again, the above code doesn't work. Instead of making a list item look like this:
<ol>
<li>
<ol>
<li></li>
...
</ol>
</li>
...
</ol>
It makes it look like this (note the closed li tag before the ol):
<ol>
<li></li>
<ol>
<li></li>
</ol>
...
</ol>
And this is the nav_adjust.php that my friend did for me which works beautifully if given the right information from index.php
$list = $_POST['list'];
$num = 1;
foreach ($list as $key => $value ) {
if ($value == "null") {
//$value = $num;
mysql_query("UPDATE `myTable` SET orderId=".$num.", parent='null' WHERE id=".$key."");
$text .= "UPDATE `myTable` SET orderId=".$num.", parent='null' WHERE id=".$key."\n\n";
} else {
$query = "UPDATE `myTable` SET orderId=". $num .", parent=".$value;
$text .= "UPDATE `myTable` SET orderId=". $num .", parent=".$value."\n\n";
$var = $list[$value];
if ( $var != null && $list[$var] != null ) {
$query .= ", grandchild = 1";
}
$query .= " WHERE id=".$key;
mysql_query($query);
}
$num++;
}
echo "Content is below\n";
print_r( $_POST['list']);
echo $text;
So, as a recap, I have a navigation that submits serialized data from index.php to nav_adjust.php which then submits it into a database (and echos it back so I know what I did). Then index.php, upon refresh, should keep the list items in place, with all children as children and such. But I overwrite index.php and have no idea how my friend did it.
My table fields are (before I forget):
id, title, orderId (parent), parent (child) and grandchild.
I was meaning to change the names of orderId and parent to make it less confusing but I never did :(
If somebody could help me, I'd be so very thankful.
If there is any other information I can give you, please ask!
After hours of struggling, I had the genius idea of asking GoDaddy if they backed up files and directories, and they did! So I got my file back. For anybody interested, the code missing on index.php looked like this (where $name['website'] is 'myTable'):
$parents = mysql_query("SELECT * FROM `".$name['Website']."` WHERE parent = 0 ORDER BY `orderId` ASC");
while($parent = mysql_fetch_array($parents)){
echo "<li id='list_".$parent['id']."'>
<div>
<span class='disclose'>
<span></span>
</span>
<span class='title'>
<a href='mod.php?id=".$parent['id']."'>".stripslashes($parent['page'])."</a>
</span>
</div>";
// Get chil elements
$children = mysql_query("SELECT * FROM `".$name['Website']."` WHERE parent = " . $parent['id'] . " ORDER BY `orderId` ASC") or die(mysql_error());
while($child = mysql_fetch_array($children)){
echo "<ol>
<li id='list_".$child['id']."'>
<div>
<span class='disclose'>
<span></span>
</span>
<span class='title'>
<a href='mod.php?id=".$child['id']."'>".stripslashes($child['page'])."</a>
</span>
</div>";
# Get grandchild elements
# (Please consider making an recursive function or so out of this if you plan to have grand grand children etc.,
# Because if you create new whiles manually for every instance, this is gonna look pretty darn ugly.
# Also, it makes it impossible to have an unknown depth.)
$grandchildren = mysql_query("SELECT * FROM `".$name['Website']."` WHERE parent = " . $child['id'] . " AND grandchild = 1 ORDER BY `orderId` ASC");
while($grandchild = mysql_fetch_array($grandchildren)){
echo "<ol>
<li id='list_".$grandchild['id']."'>
<div>
<span class='disclose'>
<span></span>
</span>
<span class='title'>
<a href='mod.php?id=".$grandchild['id']."'>".stripslashes($grandchild['page'])."</a>
</span>
</div>
</li>
</ol>\n";
}
// Close child
echo "</li>
</ol>\n";
}
// Close parent
echo "</li>\n";
}
?>

Calling an array of config items in CodeIgniter

I am new to CodeIgniter so I'm just trying to create a pretty basic site. I have 4 controllers/pages that I want to load, and a possibility to add a few more.
I have an array of items in my /applications/config/site.php file (which is autoloaded) as shown:
$site['MenuItems']['Home'] = "http://mysite.com/site/home";
$site['MenuItems']['Network Info'] = "http://mysite.com/site/info";
$site['MenuItems']['Staff'] = "http://mysite.com/site/staff";
$site['MenuItems']['Support'] = "http://mysite.com/site/support";
$config['site'] = $site;
I want to be able to take the $site['MenuItems'] array and echo out key/value pairs to place ultimately into my view page so that they are displayed as links on my site in the header. I want to be able to add and subtract items from this $site['MenuItems'] array as I need to to create more links in my header.
For example, in my view if I were to echo out the 'Home' => "http://mysite.com/site/home" key value pair:
<li>
Home
</li>
I'm not sure if I use $this->config->load('site','MenuItems') to do this...or what?
Thanks for any help you can provide me. Let me know if I'm missing something. It's probably something incredibly easy and I just can't grasp it right now :(
Controller's code:
$data['MyVarsArray'] = "That's my menu!";
$data['MyLinks'] = $this->config->item('site');
$this->load->view('myview',$data);
myview.php code:
<h2><?=$MyVarsArray?></h2>
<ul>
<?php
foreach($MyLinks['MenuItems'] as $key=>$value){?>
<li>
<?=$key?>
</li>
<?}
?>
</ul>
try this
Controller's code:
$data['MyVarsArray'] = "That's my menu!";
$data['MyLinks'] = $this->config->item('MenuItems');
$this->load->view('myview',$data);
myview.php code:
<h2><?=$MyVarsArray?></h2>
<ul>
<?php
foreach($MyLinks as $key=>$value){?>
<li>
<?=$key?>
</li>
<?}
?>
</ul>

PHP get path and all subpaths (Drupal)

I have a hard-coded menu in Drupal (as it's too complex for the standard Menu system in Drupal).
I would like to be able to say: If this page is contained within the /about/ directory, apply the class "active", so that all new pages created within this directory automatically highlight the current section.
Currently I have:
$current_page = $_SERVER['REQUEST_URI'];
<ul class="main">
<li class="home">Home</li>
<li class="about
<?php if ($current_page == "/xxxxxxx.com/dev/about/")
{
echo "active";
}
?>">About</li>
<li class="services">Services</li>
<li class="work">Work</li>
<li class="awards">Awards</li>
<li class="environment">Environment</li>
<li class="contact">Contact</li>
</ul>
I have tried a few variations of strpos and explode to get the right variable, but with no luck so far.
Thanks :)
I don't know anything about Drupal or your URL scheme, but the task of checking whether $current_page contains "/about/" you can do with:
if (strpos($current_page, '/about') !== false) echo "active";
You should probably listen to googletorp though.
Try this function. It's like arg function, but parse real path.
function real_arg($index = NULL) {
$ofset = strlen(base_path());
$q = explode('?', substr($_SERVER['REQUEST_URI'], $ofset));
$q = explode('/', trim($q[0], '/'));
return isset($index) ? $q[$index] : $q;
}
In your case:
if(real_arg(0) == 'about') echo 'active';
Use the menu api then theme your links to match what you want. You won't need to duplicate functionality that already exists. You'll acquire a skill you can reuse.
See:
http://api.drupal.org/api/function/theme_menu_item/6
http://api.drupal.org/api/function/theme_menu_item_link/6
It shouldn't take long and you'll remove a layer of workarounds.

Wordpress, PodCMS and Search

I have a WordPress site for a client. He owns a video store, and I made a site for him to update the list of movies, usually just the "new this week" movies.
I used PodCMS as an easy way for him to upload movies and then display them. No need for him to even create posts. Works really well, it's a great extension, I'm just having some issues.
The Pod has a field where you insert the release date. 2010-04-20
Then there is a Pod page/template combo that calls the movies with a certain release date like this:
$date = pods_url_variable('last');
He then just creates a blank WP page with the slug 2010-04-20
So when you open that page, the Pod page/template reads that slug and renders a list of the appropriate movies.
My problem:
I need these to be searchable. Is this possible.
I'm also open to suggestions on other ways to go about making this site work. I need it to be as simple as that. Uploads some movies and creates a new page. Then the rest is done automatically.
A PodsCMS search is nothing more than a mySQL table search for the search term. You can search the title, body, pretty much anything. Here's an example:
Note: I'm using "whatever" as the pod info. I'm also forming a string that goes into the $where position which comprises the various pods variables I want to search on. Also, I'm assuming pagination using the Pods pagination controls but I want that variable carried across the pages so I can offset.
<?php
$search_term = $_GET["s"];
$paged = get_query_var('paged');
$page_number = $_GET['pg'];
?>
<h1>
Results for "<?php echo $search_term; ?>"<?php if($page_number > 1){ ?> (Continued)<?php } ?><?php if($paged > 1){ ?> (Continued)<?php } ?>
</h1>
<?php if($paged <= 1){ ?>
<h2>Results in Whatever...</h2>
<?php
$whateverSentence = "(t.name LIKE '%" .$search_term. "%') || (t.whatever LIKE '%" .$search_term. "%')";
$whatever = new Pod('whatever');
$whatever->findRecords($orderby = 't.whatever DESC', $rows_per_page = 5, $where = $whateverSentence, $sql = null);
$total_whatever = $whatever->getTotalRows();
?>
<?php if( $total_whatever >0 ) : ?>
<?php while ( $whatever->fetchRecord() ) : ?>
<?php
// Set Variables
$whatever_ID = $whatever->get_field('id');
$whatever_Permalink = $whatever->get_field('slug');
$whatever_Name = $whatever->get_field('name');
?>
Code that echos the pods variables and represents the search result
<?php endwhile ?>
<?php else: ?>
<p>Sorry, no results found for that search.</p>
<?php endelse; endif ?>
<?php echo $whatever->getPagination($label = '<span class="pagination-text">Go to page:</span>'); ?>
<?php } ?>
So you need the WordPress page content - the list of movies - to be searchable?
WP doesn't search pages natively, but will with WordPress › Search Everything « WordPress Plugins and search better with WordPress › Relevanssi « WordPress Plugins

Categories