Simply Cutting Edge
Welcome Guest [Log In] [Register]
Welcome to zetaNetwork. We hope you enjoy your visit.

You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as viewing zetaNetwork exclusive tutorials and articles, and access to our code snippets section. Registration is simple, fast, and completely free.


Click Here To Register


If you're already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
Working with Arrays
Topic Started: Apr 18 2011, 03:34 AM (12,205 Views)
Flavius
Member Avatar
Awesome Cool Dude

Working with Arrays
Difficulty: Medium
Time: 20 - 25mins



Arrays are easily the most used way of storing data. Why? Because it's simple and easy and the data can be stored together. An array is a list of value which have a common link, and are thus put together in a list. Some things that could be stored in arrays could be the ids of rows from other tables that are directly linked to this particular row and the arrays in this rows column. If you were storing a list of friends, you would keep a list of their ids in an array.

Basic Functions:
Quote:
 
array() > denotes an array
explode('maker' , $array.var) > grabs a string of values and turns it into an array where values are separated by a defined marker
implode('maker' , $array.var) > puts arrays back into a string separating them by a defined marker
count() > will count the number of values in an array
in_array('value' , $array.var) > will check if a value is in an array

These are all straight forward to understand. So, I'll give a little demo of how something like this would be used. I will take a friend list and see how many friends there are, grab each user's data and do some checks. This would be something that's used in a social network.

Code:
 
<?php

$_SESSION['id'] = '45';
$profileid = '99';
$firendstring = '18,23,31,45,52,6,75,84,93';
$array = explode(',' , $string);
$count = count($array);
echo 'This user has '.$count.' friends';

foreach ($array as $value) {
$query = mysql_query("SELECT * FROM users WHERE id = '$value'");
while($row = mysql_fetch_assoc($query)) {
$id = $row['id'];
$name = $row['name'];
$avatar = $row['avatar'];

echo '<table><tr><td><a href="profile.php?id='.$id.'">Name</a></td></tr>
<tr><td><img src="'.$avatar.'" alt="Avatar" /></td></tr></table>';
}
}

if(in_array($_SESSION['id'])) {
echo '<a href="addfriend.php?id='.$profileid.'">Add Friend</a>';
} else {
echo 'Already a Friend';
}

?>

What this code does it quite simple. It uses the data of the person whose profile you're viewing and gets their friends list. It counts the number of friends the user has, then grabs the name and avatar of each friend and links the name to the friend's profile page. After that, it checks if you are a friend by checking for your id in this person's friend list. If you're there, then it says you're friends, if not, it gives you a link to become friends.

This tutorial is still being developed. If you have ideas as to what should be included, please send me a PM.


Flavius | zetaNetwork Instructor
If you have any questions feel free to PM me or leave the questions bellow.
Offline Profile Quote ^
 
Pro
Member Avatar
Underground Coding

Nice tutorial on PHP arrays. Looking foward to seeing the rest.
Pro | zetaNetwork Instructor & Admin
I Coded Most Of The Smexy Stuff You See :P
PM Me Any Questions
Need live support? Click here.
Offline Profile Quote ^
 
1 user reading this topic (1 Guest and 0 Anonymous)
« Previous Topic · Tutorials · Next Topic »
Add Reply