Web Hosting Talk







View Full Version : ARG! Arrays not workin?


Joshua44
11-08-2002, 07:24 PM
$dir = strtolower($board); // dir = social lounge
$file = strtolower($topic); // file = I like computers
$file = "$file 1"; // file = I like computers 1
$file = str_replace(" 1", "1", $file); // file = I like computers1
include("$dir/data.php"); // include("social lounge/data.php");

print("$topic[1]"); // prints "I"!!!!!!!!!

$total_posts++;
$file = fopen("$dir/data.php", "w");
fwrite($file, "<?
\$total_posts = \"$total_posts\";
\$total_topics = \"$total_topics\";

\$newest_date = \"$date\";
\$newest_post = \"$topic\";\n");
if($total_topics !== 0) {
$count = 1;
while($count <= $total_topics) {
fputs($file, "\$topic[$count] = \"$topic[$count]\";\n");
$count++;
}
}

fwrite($file, "\n?>");
fclose($file);




DATA FILE - /social lounge/data.php
<?
$total_posts = "11";
$total_topics = "1";

$newest_date = "8/11/2002 - 5:16.52 PM";
$newest_post = "I like computers";
$topic[1] = "I like computers";
?>

Ok, right after that include statement when I say 'print("$topic[1]");' it outputs the first letter: "I" and then prints it to the file, so it ends up:

$topic[1] = "I";

And then, for the line above it, it prints:

$newest_post = "IIlike computers";

so I'm really confused............. help please??? :(

dTuesday
11-08-2002, 08:39 PM
Originally posted by Joshua44

$dir = strtolower($board); // dir = social lounge
$file = strtolower($topic); // file = I like computers
$file = "$file 1"; // file = I like computers 1
$file = str_replace(" 1", "1", $file); // file = I like computers1
include("$dir/data.php"); // include("social lounge/data.php");

print("$topic[1]"); // prints "I"!!!!!!!!!


$file = strtolower($topic); // file = I like computers <-- shouldn't this be: // file = i like computers

Anyway, where is $topic coming from? this is the array you're try to print?

I don't know if you can use strtolower(Array); doesn't seem right to me. Perhaps this where the trouble begins.

..and shouldn't it be: $file = $file." 1"; // file = i like computers 1

I don't know. I just thought I'd take a crack at it.

- D :)

MarkIL
11-08-2002, 10:09 PM
Uhm, shouldn't the second line be

$file = strtolower($topic[1]);

?

dTuesday
11-09-2002, 03:48 AM
Yep.

- D

dTuesday
11-09-2002, 04:08 AM
Lewney,

thnx for catching that.

Joshua44,

I think the modified code below should fix your array problems.

<?php
//the include "social lounge/data.php" needs to go first.
//include("$dir/data.php"); // include("social lounge/data.php");

//--------------------------------------------------------------------
//this is the include from social lounge/data.php"
//temperarily placed here to make it easier to see whats going on.
//revert this section back to its original external include form as need be.
$total_posts = "11";
$total_topics = "1";

$newest_date = "8/11/2002 - 5:16.52 PM";
$newest_post = "I like computers";
//I don't think this next line is correct
//$topic[1] = "I like computers";

//need to change the way topic is set-up
$topic = array( "a little someth'n special", "I like computers");
//e.g. $topic[0] = "a little someth'n special" & topic[1] = "I like computers"

//let's test the array
print_r($topic);
//--------------------------------------------------------------------

$dir = strtolower($board); // dir = social lounge
$file = strtolower($topic[1]); // file = i like computers
$file = $file."1"; // file = i like computers1
//get ride of this next line not needed any more
//$file = str_replace(" 1", "1", $file); // file = i like computers1

//lets see what we've done. Is this the desired result?
echo "<br><br>"; //just adding a double line break for readability
echo "\$file = ".$file;
//print("$topic[1]"); // prints "I"!!!!!!!!!

// I didn't get past here, but I think the above should fix your problem
?>

I hope this helps.

Happy Day,

- D

dTuesday
11-09-2002, 04:13 AM
Oh and if you don't already know www.php.net is the best online referrence for this stuff.

Happy Day,

- D :D

Joshua44
11-10-2002, 02:13 PM
Yeah, I'm not an idiot! :) The only time I post here is when I'm lost and I can't find any answers. No need to direct me to another site. :)

dTuesday
11-11-2002, 03:49 PM
I never said you were an idiot. I am just trying to be helpful, no need to get defensive.

......and by the way you're welcome.

:)

-D

Joshua44
11-11-2002, 10:44 PM
lol :D thanks... I used

$varname = array("bla");

and now that works... can't figure out why the other version wouldn't work.... Oh well.