Please help... I may pull out my hair....

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Please help... I may pull out my hair....

Post by RobertGonzalez »

I feel you. I still have my editor open. I am in the middle of something at work though so it might have to wait a bit longer.
User avatar
dragonsmistress
Forum Commoner
Posts: 44
Joined: Thu Dec 18, 2008 2:03 pm

Re: Please help... I may pull out my hair....

Post by dragonsmistress »

Hey... I can wait!! I'm no longer pulling my hair out (: You've helped so much! I put a thank you to you in my site (:
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Please help... I may pull out my hair....

Post by RobertGonzalez »

This feels dirty to me for some reason, but my regex skillz are not as mad as some of my other skillz. ;)

Code: Select all

<?php
// Set the host of where things are going to get stuff from
$host = 'http://dragcave.net';
 
// Set the user - THIS WILL HOOK INTO YOUR FORM
$user = 'dragonmistress'; // Will be set by the form
 
// Set the fetch URL
$url = $host . '/user/' . $user;
 
// Get the contents of the HTML output for the user
$string = file_get_contents($url);
 
// Path builders for the regular expression and the HTML that follows
$path = '/image/';
$ext = '.gif';
 
// Regular expression pattern
$pattern = "#$path(.*?)$ext(.*?)</td><td>(Egg|Hatchling)</td><td>#mi";
 
// Do it. Do it. - A la Starsky and Hutch
preg_match_all($pattern, $string, $matches);
 
// Prepare to loop
$list = $matches[1]; // These are the image names
$type = $matches[3]; // These are the image types
$limit = count($list); // This is how many there are
 
// Loop it now and output to see how it looks
// You might want to changes this to something else in your code
for ($i = 0; $i < $limit; $i++) {
    echo $host . $path . $list[$i] . $ext . ' is a ' . $type[$i] . '<br />';
}
User avatar
dragonsmistress
Forum Commoner
Posts: 44
Joined: Thu Dec 18, 2008 2:03 pm

Re: Please help... I may pull out my hair....

Post by dragonsmistress »

this confuses me.....

there can't be a name in the script already since users will enter their own names. See what I'm saying?
User avatar
dragonsmistress
Forum Commoner
Posts: 44
Joined: Thu Dec 18, 2008 2:03 pm

Re: Please help... I may pull out my hair....

Post by dragonsmistress »

also... how can i bring up the actual image instead of the text? sorry :oops: Like my title says... newbie ):
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Please help... I may pull out my hair....

Post by RobertGonzalez »

In the comments I mention that the user name will come from the form. Or whatever mechanism you are using to fetch the username. It shouldn't be hard coded but rather come from a source. What that source is, you get to decide. :)

As for showing the image, you just want to output the html for the image, which would be something like:

Code: Select all

<?php
for ($i = 0; $i < $limit; $i++) {
    echo '<p><img src="' . $host . $path . $list[$i] . $ext . '<br />';
    echo 'Type: ' .  $type[$i] . ' <br />';
    echo 'Image: $list[$i] . $ext . '</p>'
}
User avatar
dragonsmistress
Forum Commoner
Posts: 44
Joined: Thu Dec 18, 2008 2:03 pm

Re: Please help... I may pull out my hair....

Post by dragonsmistress »

hmmmm...getting this
Parse error: syntax error, unexpected '/' in /home/hdragons/public_html/test3.php on line 35
User avatar
dragonsmistress
Forum Commoner
Posts: 44
Joined: Thu Dec 18, 2008 2:03 pm

Re: Please help... I may pull out my hair....

Post by dragonsmistress »

*cries* it's also pulling up adults as hatchlings LOL
I know this seems silly but it's my hobby. I'm a stay at home mommy so gotta do SOMETHING (:
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Please help... I may pull out my hair....

Post by RobertGonzalez »

Change the third echo statement in the loop from:

Code: Select all

echo 'Image: $list[$i] . $ext . '</p>'
to:

Code: Select all

echo 'Image: ' . $list[$i] . $ext . '</p>'
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Please help... I may pull out my hair....

Post by RobertGonzalez »

What the pattern is doing is looking for an image, then looking forward just a bit to see if it comes across either 'egg' or 'hatchling' and only pulling images that are of those two types.
User avatar
dragonsmistress
Forum Commoner
Posts: 44
Joined: Thu Dec 18, 2008 2:03 pm

Re: Please help... I may pull out my hair....

Post by dragonsmistress »

hmmm.. what do I need to look for to stop the adults from pulling up?
User avatar
dragonsmistress
Forum Commoner
Posts: 44
Joined: Thu Dec 18, 2008 2:03 pm

Re: Please help... I may pull out my hair....

Post by dragonsmistress »

Anyone? :(
User avatar
dragonsmistress
Forum Commoner
Posts: 44
Joined: Thu Dec 18, 2008 2:03 pm

Re: Please help... I may pull out my hair....

Post by dragonsmistress »

this first code that you did works better (I added some for the form and to make the images display

Code: Select all

<?php
$scrollname = $_POST['scrollname'];
if (isset($_POST['submit'])) {
}
 $string = file_get_contents('http://dragcave.net/user/' . $scrollname);
$pattern = ":/image/(.*?).gif:m";
 preg_match_all($pattern, $string, $matches);
 $list = $matches[0];
 $limit = count($list);
 for ($i = 0; $i < $limit; $i++) {
         echo "<img src=http://dragcave.net$list[$i]><br />";
}
?>
How can we add in to make it work to only pull up eggs and hatchlings? That's all this code needs (:
User avatar
dragonsmistress
Forum Commoner
Posts: 44
Joined: Thu Dec 18, 2008 2:03 pm

Re: Please help... I may pull out my hair....

Post by dragonsmistress »

*sigh* back to pulling my hair out ):
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Please help... I may pull out my hair....

Post by RobertGonzalez »

Try not to bump your own threads. It is against the rules and can get you warned around here. ;)

What denotes an egg and a hatchling vs an adult hatchling? The pattern I showed in the code I posted look specifically for the word "egg" and "hatchling" in the row it is on.
Post Reply