Welcome to WebmasterWorld Guest from 54.227.110.209

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

php/mysql newbie needs help with arrays

     

u4eas

1:40 pm on Sep 1, 2007 (gmt 0)

10+ Year Member



I hope someone can help me with this, I bet its a simple mistake I made.

What I am doing is recording how many times a link is clicked, what the id for the link is & what the name of the link is

Here is my code so far...


<?php
$id = $_GET['id'];

mysql_connect("localhost", "root", "");
@mysql_select_db("database") or die( "Unable to connect to database");
$result = mysql_query("SELECT * FROM linkcount WHERE id='$id'");

if(mysql_num_rows($result) == 0) {
mysql_query("INSERT INTO linkcount (id) VALUES ('$id','$name')")
or die(mysql_error());
mysql_query("UPDATE linkcount SET count=count+1 WHERE id='$id'")
or die(mysql_error());
} else {
mysql_query("UPDATE linkcount SET count=count+1 WHERE id='$id'")
or die(mysql_error());
}

mysql_close();

$links = array(
'google' => array('http://google.com','Google.com'),
'yahoo' =>array('http://yahoo.com', 'Yahoo.com'),
'wikipedia' => array('http://wikipedia.org', 'Wikipedia.com'),
);

header("Location:".$links[$id][$name]);

exit;
?>

I got it to record the amount of clicks and the id but I cant get it record the site name

My thought was that Yahoo=id URl=Link and Yahoo.com=Name
'yahoo' =>array('http://yahoo.com', 'Yahoo.com'),

On a side note the results pages pulls the info correctly (when I manually add the Name to the DB) but I just cant seem to insert the name via the link page.

Any ideas where I am going wrong?

thx

u4eas

dreamcatcher

2:50 pm on Sep 1, 2007 (gmt 0)

WebmasterWorld Senior Member dreamcatcher is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Hi,

What you have is a multidimensional array and you aren`t accessing the slots correctly. To see the structure of your array use print_r.

echo '<pre>';
print_r($links);
echo '</pre>';

Your array will give you something like this:

Array
(
[google] => Array
(
[0] => [google.com...]
[1] => Google.com
)

[yahoo] => Array
(
[0] => [yahoo.com...]
[1] => Yahoo.com
)

[wikipedia] => Array
(
[0] => [wikipedia.org...]
[1] => Wikipedia.com
)

)

So, to access the first slot you would need:

$links['google'][0] = [google.com...]
$links['google'][1] = Google.com

Hope that helps.

dc

u4eas

2:45 am on Sep 3, 2007 (gmt 0)

10+ Year Member



Thanks very much Ill try working with this.