Jump to content

Recommended Posts

I have the following array

 

$reputation = array (
"comments" => array (

						1 => array (
									"condition" => 1,
									"description" => "Added first Comment",
									"points" => 5
								   ),						
						2 => array (
									"condition" => 50,
									"description" => "Added 50 Comments",
									"points" => 10
								   ),
						3 => array (
									"condition" => 100,
									"description" => "Added 100 Comments",
									"points" => 25
								   )
					)
)

 

Thats stripped down but you should get the idea. I am looping through this array as follows

 

foreach($reputation as $v) {
    
    foreach($v as $vk=>$vv) {
        
?>
<tr>
    <td><?php echo $vv['points'] ?></td>
    <td><?php echo $vv['description'] ?></td>
</tr>
<?php

    }
}

 

I also have a table called rep with the following structure id, users and date with id being the same as reputation['comments'][$i]. basically I want to check to see if the user has the "achievment" and if so add the date from the database and a class to the row.

 

I am not sure on the best way to go about this, cheers for any pointers

 

The better way would be to store the array values in a database table instead of an array.

 

Sorry for my ignorance but could you explain why it would be better :)

My Take:

Because you would reduce the code significantly and you would also run the query on a database table that has at least 1 indexed field making the process more efficient and ressiliant.

I think you are looking for...

$filter = "achievment";
$query = "SELECT id, users, date FROM rep WHERE rep.users = ".$filter;
$result = mysql_query($query) or die (mysql_error));
$rep_array = mysql_fetch_assoc($result);

that's generic, and you will need to establish a connection to the database prior to including that on a page.  It will however give you an array containing the following array fields:

$rep_array['id']
$rep_array['users']
$rep_array['date']

and be populated ony with the records that match the filter.  You could effectivly just echo that list to get the same results as checking through your array (from what I gather you said you were doing with it, I may have gotten things wrong).  If that's not what you are looking for let me know, I'll be happy to work through it some more with you.

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.