Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    New Coder
    Join Date
    Apr 2012
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Image Generation from FlatFile.

    Well, I had this working perfectly until they stopped using a mysql database and started using a flatfile.
    What it should do:
    Grab data from the flatfile, trim the unwanted parts and set it in an array.
    With the received data display as an image.
    I am very confused about how I would do this.

    My current code:
    signature.php:
    Code:
    <?php
    
    //GET AND TRIM
    $fileName="accounts.txt";
    $fileHandle = fopen($fileName,"r");
    $filecontents = fread($fileHandle,filesize($fileName));
    
    $exploded = explode(" ",$filecontents);
    $name="";
    $balance="";
    $accounts=array();
    $num=0;
    for($i=0;$i<count($exploded);$i++)
    {
    
    	$line = explode("\t",$exploded[$i]);
    	for($a=0;$a<count($line);$a++)
    	{
    		
    		if(trim($line[$a])=="player")
    		{
    		}elseif($line[$a]=="money")
    		{
    		}elseif($line[$a]=="type")
    		{
    		}elseif(trim($line[$a])=="{")
    		{
    		}else
    		{
    			$subline = explode("\r",$line[$a]);
    			for($e=0;$e<count($subline);$e++)
    			{
    				if(trim($subline[$e])=="}")
    				{
    				}
    				elseif(is_numeric(trim($subline[$e])))
    				{
    					$balance=trim($subline[$e]);
    				}
    				else
    				{
    					$name=trim($subline[$e]);
    				}
    				if($name!=""&&$balance!="")
    				{
    					$temp=array($name=>$balance);
    					$accounts[$num]=$temp;	
    					$num++;
    					$name="";
    					$balance="";
    				}
    			}
    		}
    		
    	}
    	
    	
    }
    
    //Image Generation
    header("Cache-Control: no-store, no-cache, must-revalidate"); 
    header("Cache-Control: post-check=0, pre-check=0", false); 
    header("Pragma: no-cache");
    $font = "minecraft.ttf";
    $user="";
    if(isset($_GET['u']))
    {
    	if(!empty($_GET['u']))
    	{
    		$user = $_GET['u'];
    	}
    }
    
    if(isset($_GET['i']))
    {
    	if(!empty($_GET['i']))
    	{
    		$img = $_GET['i'];
    	}
    	else{$img='1';}
    }
    else{$img='1';}
    
    if(isset($_POST['u'],$_POST['i']))
    {
    	if(!empty($_POST['u']))
    	{
    		$user=$_POST['u'];
    	}
    	if(!empty($_POST['i']))
    	{
    		$img = $_POST['i'];
    	}
    }
    
    if(in_array($user, $accounts))
    {
    //Info Grabs
    $money = get_balance($user);
    //End Info Grabs
    }
    else
    {
    	die("That is an invalid player.");
    }
    
    //Display Image
    
    $user = ucfirst($user);
    
    $image = imagecreatefrompng("images/".$img.".png");
    $color = imagecolorallocate($image, 255,255,255);
    
    if ($_GET['i'] >= '8')
    {
    
    	ImageTTFText ($image, "8", 0, 100, 13, $color, $font,"Name:");
    	ImageTTFText ($image, "8", 0, 138, 13, $color, $font,$user);
    
    	ImageTTFText ($image, "8", 0, 240, 13, $color,$font,"Money:");
    	ImageTTFText ($image, "8", 0, 285, 13, $color,$font,"$".$money);
    }
    
    else
    {
    	ImageTTFText ($image, "7", 0, 246, 10, $color, $font,"Spartan Universe");
    	ImageTTFText ($image, "12", 0, 130, 50, $color, $font,"Name:");
    	ImageTTFText ($image, "12", 0, 130, 96, $color,$font,"Money:");
    	ImageTTFText ($image, "12", 0, 185, 50, $color, $font,$user);
    	ImageTTFText ($image, "12", 0, 198, 96, $color,$font,"$".$money);
    }	
    
    //End Image Display
    
    header("Content-type: image/png");
    imagepng($image);
    imagedestroy($image);
    
    ?>
    accounts.txt:
    Code:
    arkangel011 {
    	type player
    	money 23.47
    }
    chemicalbacon {
    	type player
    	money 203.89
    }
    What I would like to know is how would I go about getting the information form the array created called accounts?

  • #2
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,395
    Thanks
    8
    Thanked 1,078 Times in 1,069 Posts
    Does each player name only appear one time in the .txt file?

    Example, "chemicalbacon" only appears this one time ... ?
    chemicalbacon {
    type player
    money 203.89
    }

    Or do you have to find several "chemicalbacon" entries and add them up?

  • #3
    New Coder
    Join Date
    Apr 2012
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Only once, the file gets updated regularly adding to the number in it.

  • #4
    Regular Coder
    Join Date
    Apr 2004
    Posts
    298
    Thanks
    0
    Thanked 23 Times in 23 Posts
    Just a thought (pseudo code
    PHP Code:
    rethink your txt file ie
    arkangel011
    ,player,23.47
    chemicalbacon
    ,player,203.89

    use file() to put the lines into an array
    $lines file($textfilename);

    loop thru the array
    $count count($lines)
    for(
    $i 0$i<$count$i++) {
      
    expolde each line into a temp array
      
    $temp[$i] = explode(","$lines[$i]);
        use 
    the values in the temp array as you desire


  • #5
    New Coder
    Join Date
    Apr 2012
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I am unable to edit how the accounts.txt file stores its information.

  • #6
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,395
    Thanks
    8
    Thanked 1,078 Times in 1,069 Posts
    litebearer has the right idea ...

    You'll have to loop through the array ...

    Look for { to appear on a line,
    explode by " " and grab the name.

    Look for "type" on a line,
    explode by " " and grab the type.

    Look for "money" on a line,
    explode by " " and grab the amount.

    Look for the closing "}" on a line.
    That signals the end of that player ...

    You begin the parsing again ... looking for the next "{"

    etc.


    .

  • #7
    New Coder
    Join Date
    Apr 2012
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Right, I am not actually sure how to do that. Could I see pseudo code or an example please?

  • #8
    Regular Coder
    Join Date
    Apr 2004
    Posts
    298
    Thanks
    0
    Thanked 23 Times in 23 Posts
    a start (see if you can expand upon it)...

    PHP Code:
    $contents file_get_contents($filename);
    $my_array01 explode("}"$contents);
    print_r($my_array01); 
    ok ok...
    PHP Code:
    <?PHP
    /* for this example we are creating a string that represents the content of your file */
    $content "arkangel011 {
        type player
        money 23.47
    }
    chemicalbacon {
        type player
        money 203.89
    }"
    ;

    /*
    this how you would actually get the file contents
    $content = file_get_contents($filename);
    */

    $content str_replace("}""~"$content);
    $content str_replace("{"""$content);
    $content str_replace("type""|"$content);
    $content str_replace("money""|"$content);
    $content preg_replace("/[^a-zA-Z0-9\.\~\|]/"""$content);
    $my_array_00 explode("~"$content);
    /* note you will need to remove the last array element */
    $junk array_pop($my_array_00);
    $count count($my_array_00);
    for(
    $i=0$i<$count$i++) {
        
    $temp explode("|"$my_array_00[$i]);
        echo 
    "Name: " $temp[0] . "<br>";
        echo 
    "Type: " $temp[1] . "<br>";
        echo 
    "Money: " $temp[2] . "<hr>";

    }
    echo 
    "<PRE>";
    print_r($my_array_00);
    echo 
    "</pre>";
    ?>
    Last edited by litebearer; 04-12-2012 at 03:49 AM.

  • Users who have thanked litebearer for this post:

    EffakT (04-12-2012)

  • #9
    New Coder
    Join Date
    Apr 2012
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Okay, I will test it as soon as we can get this FTP to work properly.

  • #10
    New Coder
    Join Date
    Apr 2012
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Awesome it worked.

    Just a slight problem I have.
    Code:
    <?PHP 
    //Stop errors from displaying
    //ini_set('display_errors', 0);
    
    // Get information and trim/replace
    $name = $_GET['u'];
    $content = file_get_contents('./accounts.txt');
    $content = str_replace("}", "~", $content); 
    $content = str_replace("{", "", $content); 
    $content = str_replace("type", "|", $content); 
    $content = str_replace("money", "|", $content); 
    $content = preg_replace("/[^a-zA-Z0-9\.\~\|]/", "", $content); 
    $accounts = explode("~", $content); 
    
    //check if $name is in $accounts array
    if (in_array($name, $accounts))
    {
        $junk = array_pop($accounts); 
        $count = count($accounts); 
        for($i=0; $i<$count; $i++)
        { 
            //Display results
                $temp = explode("|", $accounts[$i]); 
                echo "Name: " . $temp[0] . "<br>"; 
                echo "Type: " . $temp[1] . "<br>"; 
                echo "Money: " . $temp[2] . "<hr>"; 
    
        } 
    }
    else
    {
    echo "Account Not Found";
    }
    ?>
    No matter what name I put in it returns Account Not Found.

  • #11
    Regular Coder
    Join Date
    Apr 2004
    Posts
    298
    Thanks
    0
    Thanked 23 Times in 23 Posts
    Try...

    PHP Code:
    <?PHP 
    //Stop errors from displaying
    //ini_set('display_errors', 0);
    // Get information and trim/replace
    $name $_GET['u'];
    $content file_get_contents('./accounts.txt');
    if(
    stristr($content$name) !== FALSE){
        
    $content str_replace("}""~"$content); 
        
    $content str_replace("{"""$content); 
        
    $content str_replace("type""|"$content); 
        
    $content str_replace("money""|"$content); 
        
    $content preg_replace("/[^a-zA-Z0-9\.\~\|]/"""$content); 
        
    $accounts explode("~"$content); 
        
    $junk array_pop($accounts); 
        
    $count count($accounts); 
        for(
    $i=0$i<$count$i++){ 
            
    //Display results
         
    $temp explode("|"$accounts[$i]); 
            if(
    $name == $temp[0]) {
                echo 
    "Name: " $temp[0] . "<br>"
                echo 
    "Type: " $temp[1] . "<br>"
                echo 
    "Money: " $temp[2] . "<hr>"
            } 
        }
    }else{
        echo 
    "Account Not Found";
    }
    ?>

  • Users who have thanked litebearer for this post:

    EffakT (04-12-2012)

  • #12
    New Coder
    Join Date
    Apr 2012
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by litebearer View Post
    Try...

    PHP Code:
    <?PHP 
    //Stop errors from displaying
    //ini_set('display_errors', 0);
    // Get information and trim/replace
    $name $_GET['u'];
    $content file_get_contents('./accounts.txt');
    if(
    stristr($content$name) !== FALSE){
        
    $content str_replace("}""~"$content); 
        
    $content str_replace("{"""$content); 
        
    $content str_replace("type""|"$content); 
        
    $content str_replace("money""|"$content); 
        
    $content preg_replace("/[^a-zA-Z0-9\.\~\|]/"""$content); 
        
    $accounts explode("~"$content); 
        
    $junk array_pop($accounts); 
        
    $count count($accounts); 
        for(
    $i=0$i<$count$i++){ 
            
    //Display results
         
    $temp explode("|"$accounts[$i]); 
            if(
    $name == $temp[0]) {
                echo 
    "Name: " $temp[0] . "<br>"
                echo 
    "Type: " $temp[1] . "<br>"
                echo 
    "Money: " $temp[2] . "<hr>"
            } 
        }
    }else{
        echo 
    "Account Not Found";
    }
    ?>
    Excellent. Thanks you so much!

    EDIT:

    Now, I put it together with the image generation portion of my original code and it displays Account Not Found again?
    Code:
    <?PHP  
    //Stop errors from displaying 
    ini_set('display_errors', 0); 
    //Stop Cache
    header("Cache-Control: no-store, no-cache, must-revalidate"); 
    header("Cache-Control: post-check=0, pre-check=0", false); 
    header("Pragma: no-cache");
    
    // Get information and trim/replace 
    $content = file_get_contents('./accounts.txt'); 
    if(stristr($content, $name) !== FALSE){ 
        $content = str_replace("}", "~", $content);  
        $content = str_replace("{", "", $content);  
        $content = str_replace("type", "|", $content);  
        $content = str_replace("money", "|", $content);  
        $content = preg_replace("/[^a-zA-Z0-9\.\~\|]/", "", $content);  
        $accounts = explode("~", $content);  
        $junk = array_pop($accounts);  
        $count = count($accounts);  
        for($i=0; $i<$count; $i++){  
            //Display results 
         $temp = explode("|", $accounts[$i]);  
    
    //Image Generation
    $font = "minecraft.ttf";
    $user="";
    if(isset($_GET['u']))
    {
    	if(!empty($_GET['u']))
    	{
    		$user = $_GET['u'];
    	}
    }
    
    if(isset($_GET['i']))
    {
    	if(!empty($_GET['i']))
    	{
    		$img = $_GET['i'];
    	}
    	else{$img='1';}
    }
    else{$img='1';}
    
    if(isset($_POST['u'],$_POST['i']))
    {
    	if(!empty($_POST['u']))
    	{
    		$user=$_POST['u'];
    	}
    	if(!empty($_POST['i']))
    	{
    		$img = $_POST['i'];
    	}
    }
    
            if($name == $temp[0]) { 
    
    //Display Image
    $user = ucfirst($user);
    $image = imagecreatefrompng("./images/".$img.".png");
    $color = imagecolorallocate($image, 255,255,255);
    
    if ($_GET['i'] >= '8')
    {
    
    	ImageTTFText ($image, "8", 0, 100, 13, $color, $font,"Name:");
    	ImageTTFText ($image, "8", 0, 138, 13, $color, $font,$temp[0]);
    
    	ImageTTFText ($image, "8", 0, 240, 13, $color,$font,"Money:");
    	ImageTTFText ($image, "8", 0, 285, 13, $color,$font,"$".$temp[2]);
    }
    
    else
    {
    	ImageTTFText ($image, "7", 0, 246, 10, $color, $font,"Spartan Universe");
    	ImageTTFText ($image, "12", 0, 130, 50, $color, $font,"Name:");
    	ImageTTFText ($image, "12", 0, 130, 96, $color,$font,"Money:");
    	ImageTTFText ($image, "12", 0, 185, 50, $color, $font,$temp[0]);
    	ImageTTFText ($image, "12", 0, 198, 96, $color,$font,"$".$temp[2]);
    }	
    
    
            }  
        } 
    header("Content-type: image/png");
    imagepng($image);
    imagedestroy($image);
    }else{ 
        echo "Account Not Found"; 
    } 
    
    ?>
    Last edited by EffakT; 04-12-2012 at 11:43 PM.

  • #13
    Regular Coder
    Join Date
    Apr 2004
    Posts
    298
    Thanks
    0
    Thanked 23 Times in 23 Posts
    well let's see... Hmmmm where are you defining $name?

  • #14
    New Coder
    Join Date
    Apr 2012
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Oh... Fail of my behalf. thanks for pointing that out


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •