Page 1 of 1

Using array values for printing links in the navigator bar

Posted: Thu Jun 22, 2006 2:07 am
by technofreak
Am trying to print a navigator bar with links in every page. I have a separate file called navig.php which takes care of printing the list.

As the number of links in each page may vary, i make use of an array in every page to store the link url and correpsonding link name to be displayed.

I pas this array as an argument to the function in navig.php which is called in everypage to display the navigator bar at the top.

But, with my code, am able to create the array but unable to use its values to print the navig bar. when i try pritning the individual array elements nothing gets printed although am able to see the contents when viewing with print_r() of the whole array.

I know am going wrong with a simple stuff, but unable to figure where ?

Heres my navig.php and the corresponding code in the main page which uses this include file...

Code: Select all

navig.php

<?php 

function navig_bar($values) {
?>

<div><table><tr>

<?php

	for ($i = 0, $num_links = count($values); $i < $num_links; $i++) {
		print '<td><a href="';
		print $values[i][0]. '" >';
		print $values[i][1]. '</a></td>';
		print '<td>&nbsp</td>';
		}

?>

</tr></table></div>

<?php } ?>

-------------------------------

page1.php

include("navig.php");

$links = array('login.php' => 'Login Page');
navig_bar($links);

print_r($links);
print $links[0];
print $links[0][0];
The print_r printed "Array ( [login.php] => Login Page )" in the browser, but not the outputs of print statements.

Posted: Thu Jun 22, 2006 2:17 am
by feyd
at minimum, "" should be [$i]

Posted: Thu Jun 22, 2006 2:55 am
by technofreak
Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


You mean

Code: Select all

print $values[i][0]. '" >';
                print $values[i][1]. '</a></td>';
should be

Code: Select all

print $values[$i][0]. '" >';
                print $values[$i][1]. '</a></td>';
??


My another question is, why am not able to print the value of an element in array $lists with

Code: Select all

print $lists[0];
??


Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Thu Jun 22, 2006 3:02 am
by JayBird
technofreak wrote: You mean

Code: Select all

print $values[i][0]. '" >';
                print $values[i][1]. '</a></td>';
should be

Code: Select all

print $values[$i][0]. '" >';
                print $values[$i][1]. '</a></td>';
??
Yes

technofreak wrote:My another question is, why am not able to print the value of an element in array $lists with

Code: Select all

print $lists[0];
??
Do this

Code: Select all

echo '<pre>';
print_r($lists);
echo '</pre>';
to see what values your Array contains

Posted: Thu Jun 22, 2006 6:00 am
by technofreak
Hey i got a clue where i might be wrong, can u confirm me ?

I am declaring an array like,

Code: Select all

$lists = array('login.php' => 'login page');
In this array, which has a key and a value pair, i can't use $lists[0] or $lists[0][0] to print the values ??

As a reply to the previous question, yes i tried printing with print_r and the array prints correctly.

I am thinking of a solution, which is..

Code: Select all

foreach ($lists as $key => $value) {
        print $key;
        print value;
}
Is this the right way to do ?

Another small query, am using PHP5 and when i try to print a carriage return using '\n" its not owrking, not "\t" either.
Has it changed or do i need to change some config ?

Code: Select all

print "This is first line\n";
print "  this is next line";
prints as "This is first line this is next line" and not as,

This is first line
this is next line

Why ?! :o

Posted: Thu Jun 22, 2006 6:18 am
by JayBird
you would need to output your array value like this

Code: Select all

print $lists['login.php'];
Or in a loop like you said

For the newline try

Code: Select all

print "This is first line\r\n";
print "  this is next line";

Posted: Thu Jun 22, 2006 11:01 am
by technofreak
For carriage return --> \r\n

Then, for tab space is it --> \r\t ??

Posted: Thu Jun 22, 2006 11:04 am
by JayBird
technofreak wrote:For carriage return --> \r\n

Then, for tab space is it --> \r\t ??
No, just \t

Posted: Fri Jun 23, 2006 1:20 am
by technofreak
Neither "/r/n" ( "/n" also din't work) nor "/t" works for me with print :( Am with no clue why its now working ??