# Testing php code

**URL:** <https://forum.kirupa.com/t/testing-php-code/183760>\
**Category:** Uncategorized\
**Created:** [March 29, 2006, 8:41pm UTC](https://forum.kirupa.com/t/testing-php-code/183760 "2006-03-29T20:41:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![takethetrain](https://avatars.discourse-cdn.com/v4/letter/t/ba8739/32.png) [@takethetrain](https://forum.kirupa.com/u/takethetrain)\
**Post date:** [March 29, 2006, 8:41pm UTC](https://forum.kirupa.com/t/testing-php-code/183760/1 "2006-03-29T20:41:08Z")

</div>

```php

<?php

/ ***************************ALL FUNCTIONS LIST*************************** /
/* */
/* Read XML File (as SimpleXMLObject) : */
/* $readXMLdata($location); */
/* */
/* Convert SimpleXML Object to Array : */
/* $arr = object2array($xml); */
/* */
/* Display Array : */
/* displayarray($array[optional]); */
/* */
/* Upload an Image and Data : */
/* loadNewData($array,$image,$thumb,$title,$description); */
/* */
/* Delete an Image (based on ID number): */
/* deleteImage($num,$array); */
/* */
/* Convert Array to XML and Write XML : */
/* array2xml($array[optional],$location); */
/* xml2file($array,$location) */
/* */
/ ************************************************************************ /

/ ***********READ XML FILE AS ARRAY*********** /

function readXMLdata($location,$xml_name) {
    if (file_exists($location)) {

        $xml_name = simplexml_load_file($location);
        } else {

        exit("Failed to open $location.");

    }

}

/* TO IMPLEMENT:

        $readXMLdata($location); */

/ ******************************************************** /

/ ***********CONVERT SIMPLEXMLOBJECT TO ARRAY*********** /

function object2array($object) 
{

    $return = NULL;

    if(is_array($object))
 {

        foreach($object as $key => $value)

        $return[$key] = object2array($value);

    } else {

        $var = get_object_vars($object);

        if($var) {

            foreach($var as $key => $value)

            $return[$key] = ($key && !$value) ? NULL : object2array($value);

        }

        else return $object; }

    return $return;

}

/* TO IMPLEMENT:

        $array = object2array($object); */

/ ******************************************************** /

/ ******************DISPLAY XML ARRAY********************* /

function displayarray($array) {

    $output = "<table><tr><td>ID</td><td width=120>IMAGE</td><td width=200>DESCRIPTION</td><td>EDIT/DELETE</td></tr>";

    $output
 .= "<tr><td height=10 bgcolor=gray></td><td height=10 bgcolor=gray></td><td height=10 bgcolor=gray></td><td height=10 bgcolor=gray></td></tr>";    
    while (list ($num, $tmp) = each ($array)) {

        $output .= "<tr>";
        $output .= "<td bgcolor=bisque><a href=\"{$tmp['image']}\">$num</a></td>";

        $output .= "<td bgcolor=cornsilk><a href=\"{$tmp['image']}\"><img src=\"{$tmp['thumb']}\" border=0><br>{$tmp['title']}</a></td>";
        $output .= "<td bgcolor=bisque>{$tmp['description']}</td>";
        $output .= "<td bgcolor=cornsilk>Edit, delete buttons</td>";

        $output
 .= "</tr><tr><td height=10 bgcolor=gray></td><td height=10 bgcolor=gray></td><td height=10 bgcolor=gray></td><td height=10 bgcolor=gray></td></tr>";    
        }

    if($array) $output .= "</table>";

    return $output;

    }

/* TO IMPLEMENT:

        displayarray($array[optional]); */

/ ******************************************************** /

/ *******************************UPLOAD IMAGES**************************** /

function loadNewData($array,$image,$thumb,$title,$description) {
//Load images and thumbs
    $image = $image . basename( $_FILES['image']['name']);

    $thumb = $thumb . basename( $_FILES['thumb']['name']);

    $_FILES['image']['tmp_name'];

    $_FILES['thumb']['tmp_name'];

    if(move_uploaded_file($_FILES['image']['tmp_name'],$image)) {

        echo "The file ". ( $_FILES['image']['name']). " has been uploaded. <br />";

    } else {

        echo "There was an error uploading the image, please try again!<br />";

    }

    
if(move_uploaded_file($_FILES['thumb']['tmp_name'], $thumb))
    {

        echo "The file ". ( $_FILES['thumb']['name']). " has been uploaded. <br />";

    } else {

        echo "There was an error uploading the thumbnail, please try again!<br />";

    }

//Load title and description
    $title = $_POST['title'];

    $description = $_POST['description'];

    echo "<b>". $title ."</b> loaded successfully. <br />";

    echo "<b>Description: </b>". $description ."<br />";

//Convert uploaded image data into an array
    $img = array('image'=>$image,'thumb'=>$thumb,'title'=>$title,'description'=>$description);

//Add image array to end of existing array

    array_push ($array, $img); 

}

/* TO IMPLEMENT:

        loadNewData($array,$image,$thumb,$title,$description); */

/ ************************************************************************ /

/***********DELETE AN IMAGE (Based on ID number)*********/

function deleteImage($num,$array) {
    $total = count($array);
    $back_del = $num - $total + 1;

    array_splice($array[$num],0);

    array_splice($array,$num,$back_del);
}

/* TO IMPLEMENT:

        deleteImage($num,$array); */

/ ******************************************************** /

/ **********CONVERT ARRAY TO XML and WRITE TO XML********* /

function array2xml($array) {

    $output = "";

    if($array) $output .= "<?xml version='1.0'?>
	<gallery>";

    while (list ($num, $tmp) = each ($array)) {

        $output .= "
		<picture>";

        while (list ($key, $val) = each ($tmp)) {

            $output .= "
			<$key>$val</$key>";

            }

        $output .= "
		</picture>";

        }

    if($array) $output .= "
</gallery>";

    return $output;

    }

/* TO IMPLEMENT:

        array2xml($array[optional]); */

/ ******************************************************** /

/ **********WRITE TO XML********* /

function xml2file($array,$location) {

    $xmlstr = array2xml($array);

    $fp = fopen ($location, 'w');

    fwrite ($fp, $xmlstr);

    fclose ($fp);

}

/* TO IMPLEMENT:

        xml2file($array[optional],$location); */

/ ******************************************************** /

?>

```

---

<div class="post-metadata">

**Author:** ![anon1712571](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@anon1712571](https://forum.kirupa.com/u/anon1712571)\
**Post date:** [March 30, 2006, 7:17am UTC](https://forum.kirupa.com/t/testing-php-code/183760/2 "2006-03-30T07:17:29Z")

</div>

:cantlook:

---

<div class="post-metadata">

**Author:** ![Seb\_Hughes](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@Seb\_Hughes](https://forum.kirupa.com/u/Seb_Hughes)\
**Post date:** [March 30, 2006, 12:08pm UTC](https://forum.kirupa.com/t/testing-php-code/183760/3 "2006-03-30T12:08:14Z")

</div>

> [@darkmotion](#):
>
> :cantlook:

:lol:
