Make your own PHP adoptables system

Need help with the site/forum/pets? Ask questions and get help from other members here.

Re: Make your own PHP adoptables system

Postby Lopunny » Sat Oct 10, 2009 2:17 pm

Thanks Nick!
User avatar
Lopunny
 
Posts: 1496
Joined: Thu Sep 03, 2009 11:34 am
My pets
My items
My wishlist
My gallery
My scenes
My dressups
Trade with me

Re: Make your own PHP adoptables system

Postby Nick » Sat Oct 10, 2009 2:54 pm

And you might consider following the directions in the last part of the tutorial and renaming your adoptables XML file to something a little harder to guess.. :D
User avatar
Nick
Admin
 
Posts: 6349
Joined: Thu Jul 03, 2008 2:40 pm
My pets
My items
My wishlist
My gallery
My scenes
My dressups
Trade with me

Re: Make your own PHP adoptables system

Postby SkysLimit » Fri Oct 16, 2009 9:44 pm

I understand everything except for one thing, how do I enter these into a website? I
I am having huge issues with this <3.
Thanks in advance!
Last edited by SkysLimit on Sat Oct 17, 2009 2:42 am, edited 3 times in total.
Image
User avatar
SkysLimit
 
Posts: 22
Joined: Fri Jan 02, 2009 4:56 pm
My pets
My items
My wishlist
My gallery
My scenes
My dressups
Trade with me

Re: Make your own PHP adoptables system

Postby Nick » Fri Oct 16, 2009 9:46 pm

It's not a good idea to attempt this tutorial unless you have a very good grasp of building HTML websites. This includes using FTP or some other tool to upload files into your webhost. The details of doing this depend entirely on where you are hosted.

You might be more comfortable building a website here instead:

http://www.myadopts.com/
User avatar
Nick
Admin
 
Posts: 6349
Joined: Thu Jul 03, 2008 2:40 pm
My pets
My items
My wishlist
My gallery
My scenes
My dressups
Trade with me

Re: Make your own PHP adoptables system

Postby SkysLimit » Sat Oct 17, 2009 2:44 am

Thanks so much for the reply!
I think I understand now.

So I uploaded the XML file and it looks fine. There is just one thing, when I try to open the page it says:
The XML page cannot be displayed
Cannot view XML input using style sheet. Please correct the error and then click the Refresh button, or try again later.


--------------------------------------------------------------------------------

XML document must have a top level element. Error processing resource 'http://paekas.net16.net/test5.xml'.


I understand that you should just see the script from what I understand.

I uploaded my image, PHP script, and xml script.

Thanks!
Image
User avatar
SkysLimit
 
Posts: 22
Joined: Fri Jan 02, 2009 4:56 pm
My pets
My items
My wishlist
My gallery
My scenes
My dressups
Trade with me

Re: Make your own PHP adoptables system

Postby Nick » Sat Oct 17, 2009 3:30 pm

There's probably something wrong with your XML script, but I can't fix it if I can't see it. You might want to PM me a link to it.
User avatar
Nick
Admin
 
Posts: 6349
Joined: Thu Jul 03, 2008 2:40 pm
My pets
My items
My wishlist
My gallery
My scenes
My dressups
Trade with me

Re: Make your own PHP adoptables system

Postby Flexxall » Thu Oct 22, 2009 4:24 am

Having an issue with part 4 of the tut.
I have had all the previous parts working but I can not get the image of the adoption that the user picked to display on the "thank you for adopting" page.
It tries to load the image then just stops and displays the rest of the text and the img src of the pet you adopted. when I view the page of the image its trying to load it gives me this error.
The image "xxx/simple1.php?a=1&v=2" can not be displayed, because it contains errors.
I have tired amending the location of the page requests in the code by adding "./" in fromt of the location and I have tried without and I get the same result each time. I have reenamed the pages a bit since I am testing such as adoptables1.xml and simple1.php but I have also changed the calles to be correct in the other files.
heres the pages i have so far. I have edited out the site address with xxx as my site is not publicly open yet.
adopt.php
Code: Select all
<html>
<head>
</head>

<body>
<?php

require("./adopttools.php");

/* Our visitor has picked an adoptable they want, its id is in $_REQUEST['id']. Find that
 * adoptable in adoptables.xml
 */
$adoptable=find_adoptable($_REQUEST['id']);

/* To randomly pick a variation from that adoptable, we're going have to do a little maths with
 * random numbers.
 *
 * First we find the sum of all the 'chance' attributes for this adoptable...
 */

$sum=0;
foreach ($adoptable->variation as $variation) {
    $sum=$sum + $variation['chance'];
}

/* Now we can generate a random number which goes from 0.0 to that sum. This will help us
 * pick a variation.
 */
$random=lcg_value()*$sum;

$sum=0;
foreach ($adoptable->variation as $variation) {
    $sum = $sum + $variation['chance'];
   
    if ($random <= $sum) {
        //Choose this variation
        $v_id=$variation['id'];
        break;
    }
}

?>

<p><img src="http://xxx.com/simple1.php?a=<?php echo $_REQUEST['id'];?>&amp;v=<?php echo $v_id;?>"></p>
<p>Thanks for adopting this pet! :). Here is the adoption code for your pet:</p>
<p><textarea cols="60" rows="2"><img src="http://xxx.com/simple1.php?a=<?php echo $_REQUEST['id'];?>&amp;v=<?php echo $v_id;?>"></textarea></p>
</body>
</html>

adoptables1.xml
Code: Select all
 <adoptables>

<adoptable id="1">
    <variation id="1" chance="1.0">
        <age image="./dogbaby.png" />
        <age date="2009-10-21" image="./dogpuppyrare.png" />
        <age date="2009-10-22" image="./dogadultrare.png" />
    </variation>
    <variation id="2" chance="5.0">
        <age image="./dogbaby.png" />
        <age date="2009-10-21" image="./dogpuppy.png" />
        <age date="2009-10-22" image="./dogadult.png" />
    </variation>
</adoptable>

<adoptable id="2">
    <variation id="1" chance="1.0">
        <age image="./ratbaby.png" />
        <age date="2009-10-21" image="./ratadult.png" />
    </variation>
</adoptable>

</adoptables>

adopttools.php
Code: Select all
<?php

//Load the adoptables XML file
$adoptxml=simplexml_load_file("./adoptables1.xml");

//Find the adoptable from adoptables.xml which has the right id
function find_adoptable($id) {
    global $adoptxml;

    //For each 'adoptable' tag which is available
    foreach ($adoptxml->adoptable as $adoptable) {
   
        //Is this adoptable's id the same as the one that the user wants to see?
        if ($adoptable['id']==$id) {
            //Yes, it is
            return $adoptable;
        }
    }
   
    return NULL; //We couldn't find an adoptable with that ID
}

//Find the variation with the given adoptable id and variation id
function find_variation($adoptid, $varid) {

    $adoptable=find_adoptable($adoptid);

    //For each 'variation' tag which is available
    foreach ($adoptable->variation as $variation) {
   
        //Is this variation's id the same as the one that the user wants to see?
        if ($variation['id']==$varid) {
            //Yes, it is
            return $variation;
        }
    }
   
    return NULL; //We couldn't find the variation with that ID
}

?>

simple1.php
Code: Select all
<?php

//Tell the browser that we are sending it a PNG image to display
header("Content-Type: image/png");

require("./adopttools.php");

//Find the variation with the given adoptable id ('a') and variation id ('v')
$variation=find_variation($_REQUEST['a'], $_REQUEST['v']);

//Decide which of the ages we want to show for the adoptable
       
$pickedAge=NULL; //We'll store the age we eventually pick into the variable $pickedAge
       
/* Check the ages in order. The age we end up picking will be the last age
 * in the file where the current date is after the 'date' attribute
 */
foreach ($variation->age as $age) {
    if (!isset($age['date']) || time() > strtotime($age['date'])) {
        $pickedAge=$age;
    }
}

//Now that we've picked the age, display the image that that age uses
readfile($pickedAge['image']);   

exit();

?>

My html web page from my phpbb3 site i am incorporating this script with
Code: Select all
    <!-- INCLUDE overall_header.html -->

<p class="{S_CONTENT_FLOW_END}<!-- IF S_USER_LOGGED_IN --> rightside<!-- ENDIF -->"><!-- IF S_USER_LOGGED_IN -->{LAST_VISIT_DATE}<!-- ELSE -->{CURRENT_TIME}<!-- ENDIF --></p>
<!-- IF U_MCP --><p>{CURRENT_TIME} <br />[&nbsp;<a href="{U_MCP}">{L_MCP}</a>&nbsp;]</p><!-- ELSEIF S_USER_LOGGED_IN --><p>{CURRENT_TIME}</p><!-- ENDIF -->

   
   
   <div class="panel">
       <div class="inner"><span class="corners-top"><span></span></span>
       <div class="content">
         <html>
<head>
</head>
<body>
<p>Adopt one of my pets!</p>
<table width="400" border="0" cellspacing="0" cellpadding="4">
  <tr>
    <td><img src="./dogbaby.png"></td>
    <td><a href="adopt.php?id=1">Adopt a dog!</a></td>
  </tr>
  <tr>
    <td><img src="./ratbaby.png"></td>
    <td><a href="adopt.php?id=2">Adopt a rat!</a></td>
  </tr>
</table>
</body>
</html>         
       </div>
       <span class="corners-bottom"><span></span></span></div>
    </div>

    <!-- INCLUDE overall_footer.html -->


Please assist me with this if you could :)
Flexxall
 
Posts: 2
Joined: Thu Oct 22, 2009 4:09 am
My pets
My items
My wishlist
My gallery
My scenes
My dressups
Trade with me

Re: Make your own PHP adoptables system

Postby Nick » Thu Oct 22, 2009 9:09 am

Hm, I can't spot the problem. Likely, the image is starting out with a PHP error message, which is why your browser can't display it. Save the broken image to disk, and open it using notepad, that will allow you to read the error message within :).

You should also check that you don't have any spaces or newlines before the <?php or after the ?> of simple.php or adopttools.php.
User avatar
Nick
Admin
 
Posts: 6349
Joined: Thu Jul 03, 2008 2:40 pm
My pets
My items
My wishlist
My gallery
My scenes
My dressups
Trade with me

Re: Make your own PHP adoptables system

Postby Flexxall » Fri Oct 23, 2009 3:17 am

Ok apparently that was the issue with a space at the end of the simple.php
Next question is there a tut or somewhere I can look to incorporate this with database usage so I can attach adoptables to the users account ?
Flexxall
 
Posts: 2
Joined: Thu Oct 22, 2009 4:09 am
My pets
My items
My wishlist
My gallery
My scenes
My dressups
Trade with me

Re: Make your own PHP adoptables system

Postby Nick » Fri Oct 23, 2009 10:03 am

Sorry, not from me.
User avatar
Nick
Admin
 
Posts: 6349
Joined: Thu Jul 03, 2008 2:40 pm
My pets
My items
My wishlist
My gallery
My scenes
My dressups
Trade with me

Who is online

Users browsing this forum: No registered users and 5 guests