SchweppesAle Posted March 10, 2009 Share Posted March 10, 2009 kind of new to php, I'm not sure why this wouldn't work though. Basically I'm creating a mod for Joomla 1.5 which will pull the latest articles then display the associated author information - preferably via the ijoomla magazine plugin. It pulls the latest articles just fine, I also added a statement which would pull the Author ID for each of the articles. The idea was that I would then place these IDs into an Array which I would then use to complete the other half of the application. unfortunately, after using the sizeof() method to check the number of elements in the array I find that it's not appending the IDs as intended. I ran a test by placing random numbers into the array before trying to throw the IDs in via the foreach statement. Clearly I'm screwing something up, have a look. <div style = "border: 1px solid #000000; margin-top: 5px; margin-left: 10px; padding: 5px; width: 500px"> <style type="text/css"> #Popular { display:none; padding: 0px; margin: 0; list-style: none; width: 100%; } #Latest{ list-style: none; padding: 0px; margin: 0; width: 100%; } </style> <div style = "width: 100%; border-bottom: 1px solid #000000; margin-bottom:3px; padding-bottom: 5px"> <div style = "float:left; width: 30%"> <a href = "#" onclick = "Display(1); return false;"> Latest </a> </div> <div style = "clear:both"></div> <div id = "Latest"> <?php $Authors[0] = "10"; $Authors[1] = "11"; function display($Authors) { global $mainframe; $db =&JFactory::getDBO(); $query = "SELECT * FROM #__content ORDER BY created DESC limit 0,11"; $db->setQuery( $query, 0, $count ); $rows = $db->loadObjectList(); echo '<ul style = "list-style:none; margin: 0px; padding: 0px">'; foreach($rows as $row) { echo "<li>$row->title ($row->publish_up)</li>"; $i = 0; $Authors[i]= "$row->created_by"; echo "Author's id: $row->created_by"; $i = $i + 1; } } display(); echo "<br/>"; $size = sizeof($Authors); echo $size; echo '</ul>'; ?> </div> <script> var Popular = document.getElementById('Popular'); var Latest = document.getElementById('Latest'); function Display(list) { switch (list) { case 1: Latest.style.display = "block"; Popular.style.display = "none"; break; case 2: Latest.style.display = "none"; Popular.style.display = "block"; break; } } </script> </div> thanks in advance, John Quote Link to comment Share on other sites More sharing options...
premiso Posted March 10, 2009 Share Posted March 10, 2009 display($Authors); You were never passing $Authors to the function as a parameter, like you have the function defined with. Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted March 10, 2009 Share Posted March 10, 2009 You are setting the array key to 0 each time Change to $i = 0; foreach($rows as $row) { echo "<li>$row->title ($row->publish_up)</li>"; $Authors[$i]= "$row->created_by"; echo "Author's id: $row->created_by"; $i = $i + 1; } Quote Link to comment Share on other sites More sharing options...
premiso Posted March 10, 2009 Share Posted March 10, 2009 You are setting the array key to 0 each time Good catch. Also $Authors[$i]= "$row->created_by"; He was setting the key to "i" not "$i" Quote Link to comment Share on other sites More sharing options...
Maq Posted March 10, 2009 Share Posted March 10, 2009 You should really format your code... After a function or a foreach loop you should indent a couple spaces to make it easy to read. You can also use $i++ rather then $i = $i + 1 Quote Link to comment Share on other sites More sharing options...
premiso Posted March 10, 2009 Share Posted March 10, 2009 Another issue, you are never returning the $Authors array after the function call. } return $Authors; } $Authors = display($Authors); Man, my brain is starting to go bad, I keep missing the simple stuff =\ Quote Link to comment Share on other sites More sharing options...
SchweppesAle Posted March 10, 2009 Author Share Posted March 10, 2009 You should really format your code... After a function or a foreach loop you should indent a couple spaces to make it easy to read. You can also use $i++ rather then $i = $i + 1 yea, I actually was actually incrementing it like that at first, I kept forgetting to add the dollar sign though for some reason and gave up though. Anyway, I just tried removed the method entirely and then adding [$i] into the array, it's still not appending the additional elements though. Really not sure why this isn't working, starting to think there's another syntax issue in there somewhere. <div style = "border: 1px solid #000000; margin-top: 5px; margin-left: 10px; padding: 5px; width: 500px"> <style type="text/css"> #Popular { display:none; padding: 0px; margin: 0; list-style: none; width: 100%; } #Latest{ list-style: none; padding: 0px; margin: 0; width: 100%; } </style> <div style = "width: 100%; border-bottom: 1px solid #000000; margin-bottom:3px; padding-bottom: 5px"> <div style = "float:left; width: 30%"> <a href = "#" onclick = "Display(1); return false;"> Latest </a> </div> <div style = "clear:both"></div> <div id = "Latest"> <?php global $mainframe; $db =&JFactory::getDBO(); $query = "SELECT * FROM #__content ORDER BY created DESC limit 0,11"; $db->setQuery( $query, 0, $count ); $rows = $db->loadObjectList(); echo '<ul style = "list-style:none; margin: 0px; padding: 0px">'; foreach($rows as $row) { echo "<li>$row->title ($row->publish_up)</li>"; $i = 0; $Authors[$i]= "$row->created_by"; echo "Author's id: $row->created_by"; $i = $i + 1; } echo "<br/>"; $size = sizeof($Authors); echo $size; echo '</ul>'; ?> </div> <script> var Popular = document.getElementById('Popular'); var Latest = document.getElementById('Latest'); function Display(list) { switch (list) { case 1: Latest.style.display = "block"; Popular.style.display = "none"; break; case 2: Latest.style.display = "none"; Popular.style.display = "block"; break; } } </script> </div> Quote Link to comment Share on other sites More sharing options...
premiso Posted March 10, 2009 Share Posted March 10, 2009 You never changed what neil pointed out. foreach($rows as $row) { echo "<li>$row->title ($row->publish_up)</li>"; //$i = 0; $Authors[$i]= "$row->created_by"; echo "Author's id: $row->created_by"; $i++; } That should increment it. Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted March 10, 2009 Share Posted March 10, 2009 Again you are setting the array key to 0 Change to $i = 0; foreach($rows as $row) { echo "<li>$row->title ($row->publish_up)</li>"; $Authors[$i]= $row->created_by; echo "Author's id: $row->created_by"; $i++; } Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted March 10, 2009 Share Posted March 10, 2009 You never changed what greenwood pointed out. Wow premiso it was almost like you read my mind. I was about to post here and clicked reply then saw 2 new posts and you mentioned my name and I hadnt even posted yet lol. edit: oh yeah meant to say I think you were referring to Maq lol. Quote Link to comment Share on other sites More sharing options...
SchweppesAle Posted March 10, 2009 Author Share Posted March 10, 2009 oh man i feel like an idiot, I kept reseting the counter XD alright, thanks a lot guys. Can't believe I didn't catch that. Quote Link to comment Share on other sites More sharing options...
premiso Posted March 10, 2009 Share Posted March 10, 2009 You never changed what greenwood pointed out. Wow premiso it was almost like you read my mind. I was about to post here and clicked reply then saw 2 new posts and you mentioned my name and I hadnt even posted yet lol. edit: oh yeah meant to say I think you were referring to Maq lol. The weirder thing it was Neil I meant to say....whoops. I must have saw your name on viewers and just did it by mistake. Sorry about that neil. Quote Link to comment Share on other sites More sharing options...
Maq Posted March 10, 2009 Share Posted March 10, 2009 Typing under the influence again...? Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted March 10, 2009 Share Posted March 10, 2009 lol and I thought I was reading under the influence there for a minute lol. Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted March 10, 2009 Share Posted March 10, 2009 The weirder thing it was Neil I meant to say....whoops. I must have saw your name on viewers and just did it by mistake Ill let you off this time Quote Link to comment Share on other sites More sharing options...
premiso Posted March 10, 2009 Share Posted March 10, 2009 Typing under the influence again...? I wish...if you call "being at work" as "under the influence" Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted March 10, 2009 Share Posted March 10, 2009 Wow and I thought that I was the only one that spent half my day "working" while I am actually browsing through and replying to threads on forums lol. Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted March 10, 2009 Share Posted March 10, 2009 I find it more interesting fixing other peoples code than what i'm supposed to be working on at work right now. Quote Link to comment Share on other sites More sharing options...
Maq Posted March 10, 2009 Share Posted March 10, 2009 Wow and I thought that I was the only one that spent half my day "working" while I am actually browsing through and replying to threads on forums lol. The slogan, "Get Addicted", has taken effect on me as well, especially while at work. Quote Link to comment Share on other sites More sharing options...
premiso Posted March 10, 2009 Share Posted March 10, 2009 I find it more interesting fixing other peoples code than what i'm supposed to be working on at work right now. Well see in my job I am basically paid to be on call. When I get a call I devote my full attention to that, or side projects etc. The problem is I do them to fast or I fix the users issue very fast and efficiently. That is what landed me this job, and I actually tried to browsing other sites for a while and just sitting here finding stuff to do. But I ran out of stuff to do within the first month and every day took forever to get over with. Thus I just browse forums and actually make the day go by much faster. My job is still priority #1 when I actually have something to do Quote Link to comment Share on other sites More sharing options...
Maq Posted March 10, 2009 Share Posted March 10, 2009 My job is still priority #1 when I actually have something to do That reminds me....... Quote Link to comment Share on other sites More sharing options...
premiso Posted March 10, 2009 Share Posted March 10, 2009 That reminds me....... Me too...time to go and get a breakfast burrito, I am starving! Quote Link to comment Share on other sites More sharing options...
Maq Posted March 10, 2009 Share Posted March 10, 2009 Sorry SchweppesAle, we totally destroyed your thread with off topic posts :-\ Did you get everything working properly? Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted March 10, 2009 Share Posted March 10, 2009 Me too...time to go and get a breakfast burrito, I am starving! At least its 15:45 in the UK, nearly home time, come on! Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted March 10, 2009 Share Posted March 10, 2009 Lol premiso mine and your jobs sound alot alike. I too am pretty much on call and go when needed and fix issues. Usually in my case it is what I like to call an id10t error lol and takes me 2 mins to fix (the only reason it takes so long is i cant seem to move the mouse fast enough lol). Quote Link to comment 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.