Jump to content

[SOLVED] two dimensional arrays


SchweppesAle

Recommended Posts

hi, I actually just posted an article on this same application. I took another look at it and decided to place both the name and id for each contributing author into the same two dimensional array. I'm having trouble fetching the name of each author based on ID from a separate user table though. You'll probably spot my problem after the second Database query. I'm having no luck with this :P

 

<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">';
$x = 0;
foreach($rows as $row)
{
	echo "<li>$row->title ($row->publish_up)</li>";

	$AuthorID[$x][0]= "$row->created_by";
	echo "Author's id: $row->created_by";
	$x++;
}

global $mainframe;
$db =&JFactory::getDBO();
$query = "SELECT * FROM #__users";
$db->setQuery( $query, 0, $count );
$rows = $db->loadObjectList();

foreach($rows as $row)
{
	if ($AuthorID[$x][0] == "$row->id")
	{
	$AuthorID[$x][1] = "$row->name";
	echo $AuthorID[$x][1];
	}
	else{$x++;}

	$x--;
}



echo "<br/>";
$size = sizeof($AuthorID);

echo "Contributing Authors:";
echo "<br/>";


?>




</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>

 

I figured that I would use the x variable as a pointer then just cycle through every row in the user table until a match is found. Unfortunately, when I added the echo $AuthorID[$x][1]; in order to test whether anything was being added, I saw that there was no output. Any suggestions are always appreciated, this forum helped me out on more than one ocassion.

Link to comment
Share on other sites

I'm not solid on multidimensional arrays just to throw that out there.

 

<?php $AuthorID[$x][0]= "$row->created_by";

Shouldn't that be the id, or what is created_by? Since you are matching it up here:

<?php if ($AuthorID[$x][0] == "$row->id") 

 

Last thing I noticed is why is there an $x--; at the end? So if it runs the else statement $x will be the same number as before. I hope that helps you out!

Link to comment
Share on other sites

I'm not solid on multidimensional arrays just to throw that out there.

 

<?php $AuthorID[$x][0]= "$row->created_by";

Shouldn't that be the id, or what is created_by? Since you are matching it up here:

<?php if ($AuthorID[$x][0] == "$row->id") 

 

Last thing I noticed is why is there an $x--; at the end? So if it runs the else statement $x will be the same number as before. I hope that helps you out!

 

the entire right side of the array should be propagated with IDs already.  [x][1].  I was trying to take each of these IDs then compare them to every row on a separate table which holds the actual user names.  the x-- was intended to recycle the previous variable which was counting up during propagation of IDs except if no match is found for the current row I threw in a x++ so basically there would be no change in the pointer as I move onto the next row. 

 

the end result should look something like this

[name][id]

[name][id], etc.

Link to comment
Share on other sites

To note, you should not be containing variables within quotes or object properties either. They are not printed strings.

 

$AuthorID[$x][0]= "$row->created_by";

$AuthorID[$x][0]= $row->created_by;

 

good call, I'm not quite sure what you meant on your previous post though.  Wouldn't $Authors[$row->id] = $row->name; just replace some  element in one dimensional array?

 

Here's what I have so far.  I removed the quotes, still not having much luck though.

 

<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">';
$x = 0;
foreach($rows as $row)
{
	echo "<li>$row->title ($row->publish_up)</li>";

	$AuthorID[$x][0]= $row->created_by;
	echo "Author's id: $row->created_by";
	$x++;
}

global $mainframe;
$db =&JFactory::getDBO();
$query = "SELECT * FROM #__users";
$db->setQuery( $query, 0, $count );
$rows = $db->loadObjectList();

foreach($rows as $row)
{
	if ($AuthorID[$x][0] == $row->id)
	{
	$AuthorID[$x][1] = $row->name;
	echo $AuthorID[$x][1];
	}
	else{$x++;}

	$x--;
}



echo "<br/>";
$size = sizeof($AuthorID);

echo "Contributing Authors:";
echo "<br/>";


?>




</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

I'm not solid on multidimensional arrays just to throw that out there.

 

<?php $AuthorID[$x][0]= "$row->created_by";

Shouldn't that be the id, or what is created_by? Since you are matching it up here:

<?php if ($AuthorID[$x][0] == "$row->id") 

 

Last thing I noticed is why is there an $x--; at the end? So if it runs the else statement $x will be the same number as before. I hope that helps you out!

 

"created_by" is a column within the _content table which indicates the ID of the user who published the article.  I'm trying to match that up with "id" which is another column within the _users table.  If the two match then we should be able to drop the "name" column of that row into the other side of the array.

Link to comment
Share on other sites

try

...
global $mainframe;
$db =&JFactory::getDBO();
$query = "SELECT * FROM #__users";
$db->setQuery( $query, 0, $count );
$rows = $db->loadObjectList();

foreach($rows as $row)
{
	for ($x = 0; $x < count($AuthorID); $x++){
		if ($AuthorID[$x][0] == $row->id)
		{
			$AuthorID[$x][1] = "$row->name";
			echo $AuthorID[$x][1];
		}
	}
}
...

but is better to select all data in one query

Link to comment
Share on other sites

Hi

 

As above, but it looks like this would be a lot easier to do by joining the 2 selected tables together and then looping through the results to populate the arrays.

 

All the best

 

Keith

 

ummm....no idea how to do that. lol 

As I stated before, I'm still relatively new to PHP and MySQL.  All I know is Select, Where, and Order ><

Link to comment
Share on other sites

try

...
global $mainframe;
$db =&JFactory::getDBO();
$query = "SELECT * FROM #__users";
$db->setQuery( $query, 0, $count );
$rows = $db->loadObjectList();

foreach($rows as $row)
{
	for ($x = 0; $x < count($AuthorID); $x++){
		if ($AuthorID[$x][0] == $row->id)
		{
			$AuthorID[$x][1] = "$row->name";
			echo $AuthorID[$x][1];
		}
	}
}
...

but is better to select all data in one query

 

thanks a lot, it looks like that worked.  I'll check into the JOIN operation after I get this up and running.  It sounds like everyone unanimously agrees that this is the worst possible way of doing things. 

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.