homepage Welcome to WebmasterWorld Guest from 54.161.192.130
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
php/mysql newbie needs help with arrays
u4eas

10+ Year Member



 
Msg#: 3438610 posted 1:40 pm on Sep 1, 2007 (gmt 0)

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

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



 
Msg#: 3438610 posted 2:50 pm on Sep 1, 2007 (gmt 0)

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

10+ Year Member



 
Msg#: 3438610 posted 2:45 am on Sep 3, 2007 (gmt 0)

Thanks very much Ill try working with this.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved