Jump to content

[SOLVED] Scope issue??


SchweppesAle

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 =\

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.  ;D

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.