Jump to content

[SOLVED] Array setup help


11Tami

Recommended Posts

Hello, how would I convert an array like this:

$thesefiles = array('something1.txt','another2.txt','3.txt');

 

Into an array where numbers are put into the array instead. I tried this but it doesn't work.

 

$thesefiles  =  new Array();

$thesefiles [1] = ["something1.txt"];

$thesefiles [2] = ["another2.txt"];

$thesefiles [3] = ["3.txt"];

 

Please let me know how to number the array in php. Thanks very much.

Link to comment
Share on other sites

first off

 

$thesefiles = array('something1.txt','another2.txt','3.txt');

 

will put numbers in

 

as for the example you did, your almost right

$thesefiles  = Array();
$thesefiles[1] = "something1.txt";
$thesefiles[2] = "another2.txt";
$thesefiles[3] = "3.txt";

 

useful command is

 

echo "<pre>";
print_r($thesefiles);

Link to comment
Share on other sites

Thank you, the code isn't reading the array number.

Here's the rest. Its supposed to go from one item in the array to the next on each visit. Anyone know why this code isn't reading the array numbers to select the next item? Thanks a lot.

 

<?php
$maximum = 2; 
$a = (isset($_COOKIE['e']) && intval($_COOKIE['e']) < $maximum)? intval($_COOKIE['e'])+1 : 1; 
setcookie("e", $a, time()+60*60*24*180); 
$currentFile = $a;
$allfiles  = Array();
$allfiles[1] = "thisfile.txt"; 
$allfiles[2] = "thatfile.html"; 
foreach($allfiles as $file):

if(preg_match("/".$currentFile."\.txt/","/".$file."\.txt/")):
	$file_content = file_get_contents($file);
	break;
elseif(preg_match("/".$currentFile."\.html/","/".$file."\.html/")):
	$file_content = file_get_contents($file);
	break;
endif;
endforeach;
echo $file_content; exit();
?>

Link to comment
Share on other sites

if your trying to pull the file from the allfiles array

 

try this

$maximum = 2; 
$a = (isset($_COOKIE['e']) && intval($_COOKIE['e']) < $maximum)? intval($_COOKIE['e'])+1 : 1; 
setcookie("e", $a, time()+60*60*24*180); 
$currentFile = $a;
$allfiles  = Array();
$allfiles[1] = "thisfile.txt"; 
$allfiles[2] = "thatfile.html";
$file_content = file_get_contents($allfiles[$currentFile]);

Link to comment
Share on other sites

That simplifies it alot Madtechie, any way I can add a .gif to it? My other one I could add .gifs to but the array wasn't numbered would like to keep the array numbered. I looked at yours inside and out and can't figure out how. Pretty sure I don't need to merge two arrays, I just need to break off a .gif from the rest somehow. Thank very much, I hope your still out there, your helping a lot.

Link to comment
Share on other sites

Looking over the posted code, you should be able to change $maximum to 3, and then just add a 3rd value to the array.

 

Untested changes to what MadTechie posted:

<?php
$maximum = 3; 
if(isset($_COOKIE['e']) && intval($_COOKIE['e']) < $maximum) {
$a = intval($_COOKIE['e'])+1;
}
else{
$a = 1; 
}
setcookie("e", $a, time()+60*60*24*180); 
$currentFile = $a;
$allfiles  = array(
1 => "thisfile.txt", 
2 => "thatfile.html",
3 => "thatfile.gif"
);
$file_content = file_get_contents($allfiles[$currentFile]);

 

Yes, I had to change the one line if/else in to a real if/else. I'm alergic to them. :P

Link to comment
Share on other sites

Thanks a lot, I sort of tried that but it wants the .gif in an image format, and I don't know where to put it.

"<img src=\'with some correct variable in here so it gets added on to the bottom with the other echo with the rest'>";

 

Also I don't think I can use file_get_contents with a .gif because I believe thats just for web page files unless I'm mistaken. Anyone know how to add the .gif? Thanks very much.

Link to comment
Share on other sites

I assume your getting data to display on the page.. if so.. this should do it

 

replace

$file_content = file_get_contents($allfiles[$currentFile]);

with

 

<?php

$getfile = $allfiles[$currentFile];
preg_match_all('/\.(.{0,3})/i', $getfile, $result, PREG_PATTERN_ORDER);
$result = $result[1];
$result = $result[count($result)-1];
$fileext = strtolower($result);

$file_content = "";
switch($fileext)
{
default:
case "html":
case "htm":
case "txt":
	$file_content = file_get_contents($getfile);
break;
case "jpg":
case "jpeg":
case "gif":
case "png":
	$file_content = "<img src=\"$getfile \" />";
break;
}

//echo $file_content;
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.