PHP - Unable to generate images from a text file

WillPower TJ

Active Member
Joined
Jan 16, 2015
Messages
62
Reaction score
0
Location
south africa
Hello everyone.
I am new to PHP and i came across this really strange problem which i tried everything in my power to fix it but could not.

So basically i have this text file("content.txt") and the PHP script is supposed to generate a html page (i.e. images, text,headings...etc) from the data provided in the text file. The text file reads as follows;


city Racing 3D~'../reviews/cityRacing3D.html'~'images/cityRacing3D.jpg'~<a href='racing.php?pg=1&load=0'>Racing</a>~<a href='publishers/3DGames.html'>3DGames</a>~42Mb~Free
Final Fantasy~'../reviews/ff3.html'~'images/ff3.jpg'~<a href='rpg.php?pg=1&load=0'>Role playing Game (RPG)</a>~<a href='publishers/square.html'>Square</a>~178.84mb~R132.00
WipeOut~'../reviews/wipeout.html'~'images/wipeout.jpg'~<a href='adventure.php?pg=1&load=0'>Adventure</a>~<a href='publishers/activision.html'>Activision Publishing, Inc</a>~35Mb~Free

.
.
.
.
repeats like this

On the PHP code,i read each individual line in ("content.txt") file and store it as an array element (on $ArrayFile[] ) and as you notice, i separated each important item with a tilde symbol(~) in the text file ,so now i just store any data before the ~ symbol into a new array ($itemData[] ), where each element in $itemData[] will contain links,source directory of images,headings ....etc.
But now what is "strange" and "weird" is that the first two images do not show. OK let me show you guys the PHP first,
i call the PHP using GET like http://pacdroids.tk/game/index.php?pg=1&load=0

Code:
<?php

$loaded =$_GET['load'];
$arrayFile = file("indexData.txt");
if ( isset($loaded) && is_numeric( $loaded )) {
	$printed = 0;  // i use this to keep track of how many <li>'s were printed , should not exceed 4
for ($loaded; $loaded < count( $arrayFile); $loaded++) { 
	$ItemData = explode("~", $arrayFile[$loaded]);
echo ("<li><a href =".$ItemData[1]."> <h3>".$ItemData[0]."</h3></a>");
echo ("<a href =".$ItemData[1]."> <img src =".$ItemData[2]." width='220' height='134' align='left' alt=".$itemData[0]."> </a>");
echo ("<p class='format'>"."Genre(s) :".$ItemData[3]."<br>".
"Publisher(s) : ".$ItemData[4]."
 <br>"."Price on Google Play :".$ItemData[6].
"<br>"."Game file size = ".$ItemData[5]."<br></p></li>");

$printed++;
if ($printed==4) {break;};
}; //end of for loop


//The above code is where the stated problem ends, You can pause here is you want.
// the following code was to let you move around previous and next page
// and remember what you printed on the previous page and not show it again.
// But it is incorrect and does not work properly. 


$page =(int)$_GET['pg'];
$lastPage= ceil( count($arrayFile)/4 );
$prevPage = $page -1;
$nextPage = $page + 1;
if ($page ===1) {
	$prevPage=null;
};
if ($page ===$lastPage) {
	$nextPage=null;};


if ($prevPage!==null) {
$loaded-=3;
	echo "<a href='index.php?page=$prevPage&load=$loaded' style='font-size:1.3em;font-weight:800;'>Prev </a>";
};


if ($nextPage!==null) {
	echo "<a href='index.php?page=$nextPage&load=$loaded' style='margin-left:100px;font-size:1.3em;font-weight:800;'>Next </a>";
};

} //end of isset if statement
else{
echo "Error";}


?>


The path to the images is correct by the way. I even tried moving the first line of data in content.txt to be the last line and moved the last line to the top, now the last line would print the image( which let me to thinking the problem can't be part the image source location).

I need help guys, i spent hours trying to spot the error but i found nothing ! any suggestion will do.
BTW i dont have any knowledge on databases, that's why i use text files. I was planning on learning as i go.

Thank you :)
 
Last edited:
It's worth learning about databases. Your text file is a little odd!

Anyway. I am on mobile so can't view source of your Web page but the PHP code you pasted doesn't add an img tag to the li tag, so there's no easy way to tell what's wrong.

Remember that images and file paths on Linux (I assume this is Linux/apache) are case sensitive.
 
Ohh god. What in the world happened here....you are right this does not add an image tag. This must be the wrong code....most of my code are similar that it confuses me sometimes.
Going to edit mode->
 
Ok. I downloaded an html source viewing app. There is a problem with the images, the "alt" attribute is incorrectly added. It should have either two double inveterted commas after the text "alt=" or you should remove it altogether.

That however is not what is causing the images not to load.

Strange, but not PHP related. Does Apache have the right permissions to read those files? It must because directly browsing to then works.
 
It's almost certainly permissions. Chmod them to 644
 
I do not know about permissions, i hosted the site on a free hosting website ( awardspace.com) so that i can test everything there. Not sure if it could be permissions though because the script loads other images well, only the first two are causing the problem.

Infact if you load that page and look carefully, you will see that the images do load ,but only for a brief while and then they magically dissapear.

It's almost certainly permissions. Chmod them to 644
I am new to PHP, i will go study the chmod and see how that works , maybe it might help. :D
 
What browser are you using?

Does it have developer tools?

If so - and the images do load (a 200 ok response) then it's not permissions.

You don't have any weird css or javascript that affects the first two li elements?
 
You don't have any weird css or javascript that affects the first two li elements?
Turns out i did :erm: . after Scudsucker mentioned the alt attribute, i went on and loaded the page and then clicked on "copy image URL" and pasted it aside and it returned "undefinded_0.jpg" and ""undefinded_1.jpg" for the two images respectively.
This then reminded me of the javascript code i was using to load different sizes of an image depending on screen size;
This was only for the first two images in a certain DIV ;
like $(".div").eq(0).attr("src","imagepath_"+number+".jpg") and $(".div").eq(1).attr("src","mobile/imagepath"+number+".jpg");

But this page was linking to that javaScript file (it uses some of the functions in it ).
So i removed the class name from that div so that the js code wont execute on it and IT WORKED!!
the JavaScript was interfering with the the li elements.
Thank you for your help sir :)
 
Last edited:
Top
Sign up to the MyBroadband newsletter
X