...

View Full Version : an odd array problem



Raraken
05-22-2004, 01:50 AM
I never used to work with array's (That's why I know so little about them), but, anyway, I have an array, and its first 2 values are always blank.

How would I correct this?

My code:
<?php
// This code is going to allow you to upload comics, and have the computer orginize them.
//lets set some array's to start off.
$file = array();
$strip = array();
if ($handle = opendir('comics/')) {
while (false !== ($file = readdir($handle))) {
$strip[] = $file;
}
closedir($handle);
}
// now, were going to run these actions for every comic, to format them to a proper array.

for ($looping=count($strip); $looping > 0; $looping --) {
//first thing to target: The comic's filename.
$filename = $strip[$looping];
//now, were going to set "Misc" to false. This means that, by default, comics won't have the "M" marking.
$misc=false;
// This will create a sudo-array, with exploded values.
list ($stripNumber, $extension) = explode ('.', $filename);
//let's check to see if the comics are marked "M"
if (substr($stripNumber, -1) == 'M'){
//if it does, we'll set is as a misc comic, and it wil be singled out and picked on by normal comics.
$misc=true;
// Now were still needing to retreive the number of the strip without that M. This will remove the M.
$stripNumber = substr($stripNumber, 0, -1);
} // End IF
// This will add a new element to the array of comics. Each element will have 3 sub-elements.
$comic[] = array('misc'=>$misc, 'stripNumber'=>$stripNumber, 'extension'=>$extension);
}
?>
<table border="1" align="center" cellpadding="1" cellspacing="1" bordercolor="#333333" bgcolor="#CCCCCC">
<tr>
<td width="130" bgcolor="#FFFFFF"> <div align="center"><font face="Arial, Helvetica, sans-serif">Comic Category </font></div></td>
<td width="130" bgcolor="#FFFFFF"> <div align="center"><font face="Arial, Helvetica, sans-serif">Strip ID </font></div></td>
<td width="130" bgcolor="#FFFFFF"> <div align="center"><font face="Arial, Helvetica, sans-serif">Image map </font></div></td>
<td width="130" bgcolor="#FFFFFF"> <div align="center"><font face="Arial, Helvetica, sans-serif">Image</font></div></td>
</tr>

<?php
$stringTag = "<td width=\"130\" bgcolor=\"#FFFFFF\"> <div align=\"center\"><font face=\"Arial, Helvetica, sans-serif\" size=\"2\">";
for ($loops=count($comic); $loops > 1; $loops --) {
echo ('<tr>');
echo ($stringTag . "&nbsp;");
if ($comic[$loops]['misc']) {
echo ("Filler Comic");
} else {
echo ("Storyline Comic");
}
echo ($stringTag . $comic[$loops]['stripNumber'] . "&nbsp;</td>");
echo ($stringTag . "." . $comic[$loops]['extension'] . "&nbsp;</td>");
echo ($stringTag . " image unavailable </td>");
echo ('</tr>');
}
?>
</table>

The output (http://www.shatterdesigns.com/comm/arrMaker.php)
NOTE: "Image Unavailable" isnt an error. I just havent added that particular portion yet.

Raraken
05-22-2004, 02:58 AM
Never mind. Instead of figuring out a way to have it add the first two, I'm just going to have it remove them, and sort em again. :P

raf
05-22-2004, 02:58 AM
i think the problem is here


if ($handle = opendir('comics/')) {
while (false !== ($file = readdir($handle))) {
$strip[] = $file;
}
closedir($handle);
}

because i would suspect that . (current dir) and .. (parent dir) are also returned.
so it should be


if ($handle = opendir('comics/')) {
while (false !== ($file = readdir($handle))) {
if (($file != '.') and ($file != '..')) {
$strip[] = $file;
}
}
closedir($handle);
}

as you or the coder would have seen if they read on to example 2 in the manual :D (i knew i'd seen that code somewhere before)
http://www.php.net/manual/en/function.readdir.php



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum