Skorpio Posted October 31, 2020 Share Posted October 31, 2020 (edited) Hi I have a 4 button navigation, home, projects, about and contact. This is in one table, menu, this table has the relevant icon to display with the appropriate link and another field is the link itself, ?page=home. Menu is joined with the pages table using the following query SELECT * FROM menu LEFT JOIN pages ON menu.id = pages.page_id When I run this through phpmyadmin sql console everything is returned as expected, all pages match up with the navigation. When I print_r($row); all 4 fields from the nav table and all 7 fields from the pages table, totalling 11 fields, display as per the query running in phpmyadmin which was as expected however, on the actual page it isn't going as hoped and expected. I have /* query for page data(navigation & title) */ $sql= "SELECT * FROM menu LEFT JOIN pages ON menu.id = pages.page_id"; $stmt = $db->query($sql); $row = $stmt->fetchAll(PDO::FETCH_ASSOC); /* to go in php tags where table data required - <?php echo $row['header']; ?> */ // Sidenote, I realise prepare rather than query but need to get this working first echo "<pre>"; print_r($row); echo "</pre>"; // Everything, all records from both tables, showing up as expected in array If I use $row = $stmt->fetch(PDO::FETCH_ASSOC); As expected I only get 1 record returned, all nav buttons are home which is the 1 record with all page data from the pages table and all icons from the menu table are the home icon, again as expected since it is the 1 record. When I use fetchAll everything is returned undefined index, e.g. Undefined index: content3 in C:\xampp\htdocs\test\index.php on line 70 The php used for the navigation is <?php echo $row['url']; ?> <?php echo $row['icon']; ?> <?php echo $row['header']; ?> the full link is <a href="<?php echo $row['url']; ?>"><i class="<?php echo $row['icon']; ?>"></i> <?php echo $row['header']; ?></a> The above is listed 4 times for Home, Projects, About and contact. As I know the SQL is working I realise I have made an error in the PHP but cannot work out what. As far as I can see I should be using fetch, rather than fetchAll, so as to only return 1 record as requested via the navigation links however fetchAll would give me the data required for the navigation to be labelled correctly. I cannot work out where I am going wrong. Any help would be appreciated. Edited October 31, 2020 by Skorpio Update with further information Quote Link to comment https://forums.phpfreaks.com/topic/311663-php-pdo-query-issue-for-navigation-and-content-from-mysql-db/ Share on other sites More sharing options...
Skorpio Posted October 31, 2020 Author Share Posted October 31, 2020 In addition, the returned array when using fetch all. Quote Link to comment https://forums.phpfreaks.com/topic/311663-php-pdo-query-issue-for-navigation-and-content-from-mysql-db/#findComment-1582162 Share on other sites More sharing options...
Barand Posted October 31, 2020 Share Posted October 31, 2020 Illegible. Quote Link to comment https://forums.phpfreaks.com/topic/311663-php-pdo-query-issue-for-navigation-and-content-from-mysql-db/#findComment-1582163 Share on other sites More sharing options...
Skorpio Posted October 31, 2020 Author Share Posted October 31, 2020 (edited) What is? As I say the SQL is working, it is the PHP thats the issue. Edited October 31, 2020 by Skorpio Quote Link to comment https://forums.phpfreaks.com/topic/311663-php-pdo-query-issue-for-navigation-and-content-from-mysql-db/#findComment-1582164 Share on other sites More sharing options...
dodgeitorelse3 Posted October 31, 2020 Share Posted October 31, 2020 Very very hard to see the returned array Quote Link to comment https://forums.phpfreaks.com/topic/311663-php-pdo-query-issue-for-navigation-and-content-from-mysql-db/#findComment-1582165 Share on other sites More sharing options...
Skorpio Posted October 31, 2020 Author Share Posted October 31, 2020 Apologies, here is the array Quote Link to comment https://forums.phpfreaks.com/topic/311663-php-pdo-query-issue-for-navigation-and-content-from-mysql-db/#findComment-1582166 Share on other sites More sharing options...
Skorpio Posted October 31, 2020 Author Share Posted October 31, 2020 Although still looking to resolve the issue with my php above I am looking at other solutions. Although the followi8ng code populates my navigation menu correctly I am unable to get the page data to sync with the appropriate navigation choice. $sql= "SELECT * FROM menu LEFT JOIN pages ON menu.id = pages.page_id"; $stmt = $db->query($sql); $row = $stmt->fetchAll(PDO::FETCH_ASSOC); // To print array out echo "<pre>"; print_r($row); echo "</pre>"; Then where the navigation is I have <?php foreach($db->query($sql) as $row){ ?> <a href="<?php echo $row['url']; ?>"><i class="<?php echo $row['icon']; ?>"></i> <?php echo $row['header']; ?></a> <?php } ?> As I say the nav menu is returned correctly but I am unable to sync with the corresponding page from the pages table, the join SQL statement above returns the full array from both tables synced but the issue I am having is the closing brace. I am unable to place it further down the page to enable all the relevant areas of the page to echo the relevant data from the pages table. As per screenshot2 attached it is the placement of the closing brace preventing me from having the corresponding navigation selection and corresponding page showing as the data areas for the pges table are outside of the foreach . Quote Link to comment https://forums.phpfreaks.com/topic/311663-php-pdo-query-issue-for-navigation-and-content-from-mysql-db/#findComment-1582167 Share on other sites More sharing options...
requinix Posted October 31, 2020 Share Posted October 31, 2020 I can't tell: are you still trying to solve the undefined index problem? When you use fetchAll, $row (that being the variable you chose to assign the results to) will be an array of arrays. You need to loop over that just like you did with ->query. If you write that code out you'll get foreach ($row as $row) { Now, doesn't that look a little weird? Quote Link to comment https://forums.phpfreaks.com/topic/311663-php-pdo-query-issue-for-navigation-and-content-from-mysql-db/#findComment-1582170 Share on other sites More sharing options...
Skorpio Posted October 31, 2020 Author Share Posted October 31, 2020 I am trying to resolve the initial problem, getting nowhere fast. so it should read $ 8 hours ago, Skorpio said: $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); As the variable is storing all data records? So 11 minutes ago, requinix said: foreach ($row as $row) { Should read foreach ($row as $rows){ Is that what you are meaning requinix? The foreach that I am using in the 2nd example 1 hour ago, Skorpio said: foreach($db->query($sql) as $row){ ?> <a href="<?php echo $row['url']; ?>"><i class="<?php echo $row['icon']; ?>"></i> <?php echo $row['header']; ?></a> <?php } ?> works as far as the navigation however I cannot include the content for the page within the foreach which makes the join redundant. I am unsure what the solution is to this but I feel as though I am going round in circles. 8 hours ago, Skorpio said: $sql= "SELECT * FROM menu LEFT JOIN pages ON menu.id = pages.page_id"; $stmt = $db->query($sql); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); The above, my first code with the foreach loop would present me with the same issue of not being able to sync the page contact with the navigation. Quote Link to comment https://forums.phpfreaks.com/topic/311663-php-pdo-query-issue-for-navigation-and-content-from-mysql-db/#findComment-1582171 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.