Jump to content

[SOLVED] Array and foreach statement


iarp

Recommended Posts

<div id="menu">
<ul id="main">
	<li<?php if ($_SERVER['REQUEST_URI'] == "/"){ currentPage($_SERVER['REQUEST_URI']); } ?>><a href="/">Homepage</a></li>
	<li<?php if ($_SERVER['REQUEST_URI'] == "/?id=services"){ currentPage($_SERVER['REQUEST_URI']); } ?>><a href="/?id=services" title="Services Offered">Services & Pricing</a></li>
	<li<?php if ($_SERVER['REQUEST_URI'] == "/?id=solutions"){ currentPage($_SERVER['REQUEST_URI']); } ?>><a href="/?id=solutions" title="Tech Solution's"> Free Solutions </a></li>
	<li<?php if ($_SERVER['REQUEST_URI'] == "/?id=contact"){ currentPage($_SERVER['REQUEST_URI']); } ?>><a href="/?id=contact" title="To reach us">Contact Us</a></li>
	<?php if (!isset($_SESSION['user_id'])){ echo "<li"; if ($_SERVER['REQUEST_URI'] == "/?id=register") currentPage($_SERVER['REQUEST_URI']); echo "><a href=\"/?id=register\" title=\"To register\">Register</a></li>";} ?>
</ul>
</div>

 

The above code is what i had as my navigation but wanted to make it easier to change rather then editing the header page. I tried the code below, but all i get back is 'HHhh'

 

$query = "SELECT page_name, page_url FROM " . TBL_CONTENT;
$result = mysql_query($query);
$content = mysql_fetch_array($result);
//echo $content['page_name'];

foreach ($content as $msg) {
echo $msg['page_name'];
}

 

I've never been good with arrays and foreach statments. Theres a reason why it isn't just grabbing all the page names and urls but i don't know why.

 

Any helps appreciated.

Link to comment
https://forums.phpfreaks.com/topic/115532-solved-array-and-foreach-statement/
Share on other sites

DEFINE ('TBL_CONTENT', 'IARP_content');

 

CREATE TABLE `IARP_content` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `page_name` varchar(90) NOT NULL default '',
  `page_url` varchar(90) NOT NULL default '',
  `content` longtext NOT NULL,
  `editor` varchar(20) NOT NULL default '',
  `last_edited` varchar(60) NOT NULL default '0000-00-00 00:00:00',
  `created` datetime NOT NULL default '0000-00-00 00:00:00',
  `parent` varchar(25) NOT NULL default '',
  `active` tinyint(1) NOT NULL default '0',
  PRIMARY KEY  (`id`),
  UNIQUE KEY `page_url` (`page_url`)
) TYPE=MyISAM AUTO_INCREMENT=14 ;

foreach ($content as $msg) {
echo $msg['page_name'];
}

 

$content contains an array of the fields in that row eg

[pre]

array (

  'page_name' => 'xxx',

  'page_url => 'yyy'

)[/pre]

 

When you use

foreach ($content as $msg) 

the value of the fields are put in $msg, so you need

foreach ($content as $msg) {
echo $msg;
}

 

I don't understand why it only returns 1 row of values(and echos them twice)

 

All i get is "HomeHomehomehome" ahla:

 

$query = "SELECT page_name, page_url FROM " . TBL_CONTENT;
$result = mysql_query($query);
$content = mysql_fetch_array($result);
//echo $content['page_name'];

foreach ($content as $msg) {
echo $msg;
}

mysql_fetch_array gets the fields from the row and stores them in an array with both a numeric index and the field name as an index.

 

Have look at what is returned with

echo '<pre>', print_r($content, 1), '</pre>';

 

If you are going to use foreach on the fields, use mysql_fetch_assoc() or mysql_fetch_row()

i get:

 

Array

(

    [page_url] => contact

)

 

Using

 

mysql_fetch_assoc() and echo '<pre>', print_r($content, 1), '</pre>';

 

Shouldn't:

 

$query = "SELECT page_url FROM " . TBL_CONTENT;

$result = mysql_query($query);

$content = mysql_fetch_assoc($result);

//$content = $content['page_url'];

 

Grab every row?

After a bit of editing i got it to finally work:

 

$query = "SELECT page_name, page_url, main_nav FROM " . TBL_CONTENT;
if ($result = mysql_query($query)) {
  if (mysql_num_rows($result)) {
	while ($row = mysql_fetch_assoc($result)) {
		if ($row['main_nav'] == TRUE){
		echo '<li'; 
			if ($url == "/?id=" . $row['page_url'] . ""){ 
				currentPage($url); 
			} 
		echo ">";
		echo '<a href="/?id='. $row['page_url'] . '">' . $row['page_name'] . '</a></li>';
		}
	}
  }
}

 

Solved, thanks.

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.