Jump to content

[SOLVED] Arrays...


plutomed

Recommended Posts

I have an array with some bits in and it don't echo out properly.

 


$footer = array(
	"names" => array("About Us", "Site Map", "Privacy Policy", "Contact Us"),
	"pages" => array(prefix()."index.php?pm=about_us", prefix()."index.php?pm=site_map", "javascript:win_open('".prefix()."includes/privacy_policy.php', 'Privacy_policy', 'directories=0, height=200, location=0, menubar=0, resizable=0, scrollbars=0, status=0, toolbar=0, width=500');", prefix()."index.php?pm=contact_us")
);
$count_footer = count($footer['names']);
$i_footer = 0;

while($i_footer < $count_footer)
{
	$links_footer .= " <a href=\"".$footer['pages'][$i_footer]."\">".$footer['names'][$i_footer]."</a> |";
	$i_footer++;
}
echo $links_footer;

 

This is what it echo's:

 

/new//new//new//new/ About Us | Site Map | Privacy Policy | Contact Us |

 

The prefix is in the wrong place. How come?

Link to comment
https://forums.phpfreaks.com/topic/74312-solved-arrays/
Share on other sites

That is why. When you "echo" a string it goes directly to the browser screen, you need to return the string if you want to have it put into the string.

 

Write it something like this:

<?php
function prefix() {
   return ('/new/');
}
?>

 

BTW, I tightened up your code a little:

<?php
$footer = array("names" => array("About Us", "Site Map", "Privacy Policy", "Contact Us"),
                "pages" => array(prefix()."index.php?pm=about_us", prefix()."index.php?pm=site_map", "javascript:win_open('".prefix()."includes/privacy_policy.php', 'Privacy_policy', 'directories=0, height=200, location=0, menubar=0, resizable=0, scrollbars=0, status=0, toolbar=0, width=500');", prefix()."index.php?pm=contact_us"));
$count_footer = count($footer['names']);
$i_footer = 0;
$tmp = array();
for ($i_footer=0;$i_footer<count($footer['names']);$i_footer++)
        $tmp[] = ' <a href="' . $footer['pages'][$i_footer] . '">' . $footer['names'][$i_footer] . '</a>';
echo implode(' | ',$tmp);
?>

 

Using an array to temporarily store the footer string and then using the implode function puts the " | " between the entries and you don't have a trailing " | ".

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/74312-solved-arrays/#findComment-375470
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.