[thelist] PHP coding help

R.Livsey R.Livsey at cache-22.co.uk
Fri Sep 7 07:57:08 CDT 2001


The following works fine for me.
Changed a couple of things which I have commented on in the file.

When you increment 00006 it returns 7 ie the 0's are lost. The pad
function fixes this.


<?php

$readID = "";
$id = "";
$dataID = "";
$writeID = "";
$filename = "../xml/id_list.xml";

//Open & read the XML doc
     $f = fopen($filename, "a+");
     $readID = fread($f, filesize($filename));
     fclose($f);

     /* unlink($filename);  dont need to unlink file */


//Open & write to the XML doc

     $id = substr($readID, strlen($readID)-20, 5);  

        echo "$id<br>";       /* Returns 00006 */

     $id = $id + 1;         

        echo "$id<br>";       /* Returns 7 */

	 $id = pad($id, 5, '0');

        echo "$id<br>";       /* Return 00007 */

     $dataID = " <id>".$id."</id>\n</id_list>"; /* added space and \n so
that the file stays the same layout */

        echo "$id_data<br>";   /* Returns <id>00007</id></id_list> */

     $f = fopen($filename, "w"); /* moved down here as dont need it open
earlier, changed to 'w' so overwrites old file*/

     $writeID = str_replace("</id_list>",$dataID,$readID); 
     fwrite($f, $writeID);
     fclose($f);


	/* pads a string $to_pad upto length $length padding with
$pad_char. */
	function pad($to_pad, $length, $pad_char)
	{
		while (strlen($to_pad)<$length) 
		{
		    $to_pad = $pad_char . $to_pad;
		}
		return $to_pad;
	}
?>

Hope this helps.

R.Livsey
------------------------------------------
If tomorrow never comes, then, you're dead.
------------------------------------------
Lead Programmer for the Tickle Group
Freelance work always welcome
[ PHP | Perl | mySQL | Java ]
w : cache-22.co.uk
e : R.Livsey at cache-22.co.uk
m : +447764 685 701
i  : 37530949 


> -----Original Message-----
> From: thelist-admin at lists.evolt.org 
> [mailto:thelist-admin at lists.evolt.org] On Behalf Of dante
> Sent: 07 September 2001 12:49
> To: thelist at lists.evolt.org
> Subject: Re: [thelist] PHP coding help
> 
> 
> Thanks,
> I've almost got it working.
> What I've got do far:
> I can't seem to grasp what it is I'm missing - a typo or something?
> 
> Thanks,
>             dante            
>             Perth, Western Australia
>             dante at vianet.net.au
>             www.vianet.net.au/~dante
> 
> 
> ---------------------------------------
> For unsubscribe and other options, including
> the Tip Harvester and archive of TheList go to: 
http://lists.evolt.org Workers of the Web, evolt ! 





More information about the thelist mailing list