Jump to content

My Variables Nightmare


jonathan.Theaker

Recommended Posts

Hello,

 

I was given this piece of code, it basically scrapes data from another website then prints the requested data on the loaded page, unfortunately I need to be able to pick out exactly which bits of the data I want printing out instead of all of it. I've been told I can achieve this by storing the scraped data in variables then calling them up... but I have no idea what that means and how I would go about implementing this. could anyone help me achieve this?

 

Here is the code:

 

<?php

//by RobbieThe1st

function ge_info($id) {

$ctime = microtime(true);

$ch = curl_init();

curl_setopt($ch,CURLOPT_URL,'http://itemdb-rs.runescape.com/viewitem.ws?obj='.$id);



curl_setopt($ch,CURLOPT_TIMEOUT,10);

curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12');

curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

$file = curl_exec($ch);

curl_close($ch);

$out = array();

$v000 = strpos($file,'<div class="subsectionHeader">',0) + 30;

$v00 = strpos($file,'<div class="subsectionHeader">',$v000) + 30;

$v0 = strpos($file,'<div class="subsectionHeader">',$v00) + 31;

$v1 = strpos($file,'</div>',$v0) + 6;

$out['name'] = trim(substr($file,$v0,($v1 - 6) - $v0));

if($out['name'] == 'Error</div>') { return false; };

$code = explode(chr(10),substr($file,$v1,strpos($file,'</div>',$v1) - $v1));

$out['examine'] = trim($code[3]);

$find = array('Minimum price:','Market price:','Maximum price:','7 Days:','30 Days:');

$names = array('low_price','market_price','max_price','7d_change','30d_change'

);

$fN = 0;

for($i=0, $count = count($code); $i<$count; ++$i) {

if($v1 = strpos($code[$i],$find[$fN])) {

$out[$names[$fN]] = str_replace(array(',','+','%'),false,
trim(strip_tags(substr($code[$i],$v1 + strlen($find[$fN])))));

$fN ++;

if($fN == 5) { break; };

};

};

return $out;

};



print_r(ge_info(2));

?>

 

I couldn't thank anyone enough that can offer me some insight or an example.

 

Regards

Jonathan

Link to comment
Share on other sites

you can find this script running at http://hintscape.net/x.php i would like to be able to display just one of the following "Dragon 2h sword" or "A two-handed Dragon Sword" or "1.5m" "1.6m"  or "1.7m" .

 

Basicly display the outcome of [name], [examine], [low_price], [market_price] & [max_price] but each needs to be able to be displayed on its own with out the others so i can enter each outcome in to a table like this one http://www.tip.it/runescape/index.php?rs2item_id=4568

Thanks for the reply

 

Regards

Jonathan

Link to comment
Share on other sites

Replace the:

 

print_r(ge_info(2));

at the bottom with:

 

echo "Item name: ".ge_info(2)['name'];

If you can understand the basics of using echo() then you should be on your way.

 

Ok so i have read a tutorial on how to create strings and echo them, ive had a play and i keep getting errors. if i try your exact example above i get the following error:

 

Parse error: syntax error, unexpected '[', expecting ',' or ';' in /homepages/8/d321609305/htdocs/hintscape.net/x.php on line 82

 

Again am I just being a numb skull or incredibly stupid...

 

thanks for persevering!

Link to comment
Share on other sites

ya, this:

 

ge_info(2)['name']

 

isn't going to work.

 

Advice: since you're a newbie, it's no better of a time to start practicing good coding styles/formatting, especially when seeking help/advice.  what i mean is, it is very hard to read code where every line starts at the first character space.  make sure and use indenting in your code.

Link to comment
Share on other sites

Oh and here is the cleaner indented code encase it is usefull to anyone else.

 

<?php
//by RobbieThe1st
function ge_info($id) {
	$ctime = microtime(true);
	$ch = curl_init();
	curl_setopt($ch,CURLOPT_URL,'http://itemdb-rs.runescape.com/viewitem.ws?obj='.$id);
	curl_setopt($ch,CURLOPT_TIMEOUT,10);
	curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12');
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
	$file = curl_exec($ch);
	curl_close($ch);
	$out = array();
	$v000 = strpos($file,'<div class="subsectionHeader">',0) + 30;
	$v00 = strpos($file,'<div class="subsectionHeader">',$v000) + 30;
	$v0 = strpos($file,'<div class="subsectionHeader">',$v00) + 31;
	$v1 = strpos($file,'</div>',$v0) + 6;
	$out['name'] = trim(substr($file,$v0,($v1 - 6) - $v0));

	if($out['name'] == 'Error</div>') {
		return false;
	}

	;
	$code = explode(chr(10),substr($file,$v1,strpos($file,'</div>',$v1) - $v1));
	$out['examine'] = trim($code[3]);
	$find = array('Minimum price:','Market price:','Maximum price:','7 Days:','30 Days:');
	$names = array('low_price','market_price','max_price','7d_change','30d_change');
	$fN = 0;
	for($i=0, $count = count($code); $i<$count; ++$i) {

		if($v1 = strpos($code[$i],$find[$fN])) {
			$out[$names[$fN]] = str_replace(array(',','+','%'),false,                        trim(strip_tags(substr($code[$i],$v1 + strlen($find[$fN])))));
			$fN ++;

			if($fN == 5) {
				break;
			}

			;
		}

		;
	}

	;
	return $out;
}

;
?>

Thanks

Link to comment
Share on other sites

My advice on books are these two:

 

PHP and MySQL Web Development - Fourth Edition

Wicked Cool PHP

 

PHP for dummies isn't very intuitive and leaves beginner programmers stranded or confused a lot of the time. If you pick up the two I suggested you'll be on your way in no time :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.