iarp Posted July 18, 2008 Share Posted July 18, 2008 <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 More sharing options...
CaptainChainsaw Posted July 18, 2008 Share Posted July 18, 2008 Might be a problem with this line: $query = "SELECT page_name, page_url FROM " . TBL_CONTENT; What does "TBL_CONTENT" contain? CC Link to comment https://forums.phpfreaks.com/topic/115532-solved-array-and-foreach-statement/#findComment-593923 Share on other sites More sharing options...
iarp Posted July 19, 2008 Author Share Posted July 19, 2008 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 ; Link to comment https://forums.phpfreaks.com/topic/115532-solved-array-and-foreach-statement/#findComment-593926 Share on other sites More sharing options...
Barand Posted July 19, 2008 Share Posted July 19, 2008 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; } Link to comment https://forums.phpfreaks.com/topic/115532-solved-array-and-foreach-statement/#findComment-594083 Share on other sites More sharing options...
iarp Posted July 19, 2008 Author Share Posted July 19, 2008 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; } Link to comment https://forums.phpfreaks.com/topic/115532-solved-array-and-foreach-statement/#findComment-594483 Share on other sites More sharing options...
Barand Posted July 20, 2008 Share Posted July 20, 2008 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() Link to comment https://forums.phpfreaks.com/topic/115532-solved-array-and-foreach-statement/#findComment-594485 Share on other sites More sharing options...
iarp Posted July 20, 2008 Author Share Posted July 20, 2008 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? Link to comment https://forums.phpfreaks.com/topic/115532-solved-array-and-foreach-statement/#findComment-594493 Share on other sites More sharing options...
Barand Posted July 20, 2008 Share Posted July 20, 2008 No, each time you call a mysql_fetch_xxx() function it reads the next row. www.php.net/mysql_fetch_assoc Link to comment https://forums.phpfreaks.com/topic/115532-solved-array-and-foreach-statement/#findComment-594498 Share on other sites More sharing options...
DarkWater Posted July 20, 2008 Share Posted July 20, 2008 Basically: while($row = mysql_fetch_assoc($result)) { echo $row['page_name']; } Link to comment https://forums.phpfreaks.com/topic/115532-solved-array-and-foreach-statement/#findComment-594505 Share on other sites More sharing options...
iarp Posted July 20, 2008 Author Share Posted July 20, 2008 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. Link to comment https://forums.phpfreaks.com/topic/115532-solved-array-and-foreach-statement/#findComment-594530 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.