Jump to content

Better method?


-Karl-

Recommended Posts

			if ($index != "") {
   				$lulwut = explode("\n", $index);
   				$overall = explode(",", $lulwut[0]);
   				$lol['att'] = explode(",", $lulwut[1]);
   				$lol['str'] = explode(",", $lulwut[2]);

				$lol['att'] = str_replace('-1', '1', $lol['att']);
				$lol['str'] = str_replace('-1', '1', $lol['str']);

				$hiscores = $db->query("UPDATE `clan_members` SET 
						`attack` 	= '".$lol['att'][1]."',	
						`strength` 	= '".$lol['str'][1]."'
					WHERE `username` = '$username'
				") or die(mysql_error());


		}

 

That's my code, however there's another 23 of the strings. I've only showed attack and strength. Is there another way to do this?

 

Also, I just posted this and received a warning for double posting. God knows how.

Link to comment
https://forums.phpfreaks.com/topic/201495-better-method/
Share on other sites

Tidy it up man.. makes it easier for people on the forum to read.

if ($index != "") {
      $lulwut = explode("\n", $index);
      $overall = explode(",", $lulwut[0]);
      $lol['att'] = explode(",", $lulwut[1]);
      $lol['str'] = explode(",", $lulwut[2]);

   $lol['att'] = str_replace('-1', '1', $lol['att']);
   $lol['str'] = str_replace('-1', '1', $lol['str']);

   $hiscores = $db->query("UPDATE `clan_members` SET 
         `attack`    = '".$lol['att'][1]."',   
         `strength`    = '".$lol['str'][1]."'
      WHERE `username` = '$username'
   ") or die(mysql_error());
}

 

That's my code, however there's another 23 of the strings.

Oh my god, really? Thats your code! Amazing.. ;)

 

Anyway, being as $lol, is an associative array. You could do this.

foreach($lol as $key => $value) {
    $$key = str_replace('-1', '1', $value);
}

That would be in replacement of the str_replaces.

You would use $$key, for example.. $lol['att']. 'att' is $key. So $$key, would be $att

You'd use it as echo $att;

 

So when you come to the query, it'll be $att, instead of $lol['att']

 

I'm not sure if it's correct.. but I tried my best and hopefully it may work.

Link to comment
https://forums.phpfreaks.com/topic/201495-better-method/#findComment-1057144
Share on other sites

		$index = @file_get_contents('http://hiscore.runescape.com/index_lite.ws?player=' . $username);

		if ($index != "") {
   				$lulwut = explode("\n", $index);
   				$overall = explode(",", $lulwut[0]);
   				$lol['att'] = explode(",", $lulwut[1]);
   				$lol['def'] = explode(",", $lulwut[2]);
   				$lol['str'] = explode(",", $lulwut[3]);
   				$lol['hp'] = explode(",", $lulwut[4]);
   				$lol['rng'] = explode(",", $lulwut[5]);
   				$lol['pry'] = explode(",", $lulwut[6]);
   				$lol['mag'] = explode(",", $lulwut[7]);
   				$lol['ck'] = explode(",", $lulwut[8]);
   				$lol['wc'] = explode(",", $lulwut[9]);
   				$lol['flt'] = explode(",", $lulwut[10]);
   				$lol['fsh'] = explode(",", $lulwut[11]);
   				$lol['fm'] = explode(",", $lulwut[12]);
   				$lol['cra'] = explode(",", $lulwut[13]);
   				$lol['smi'] = explode(",", $lulwut[14]);
   				$lol['min'] = explode(",", $lulwut[15]);
   				$lol['her'] = explode(",", $lulwut[16]);
   				$lol['ag'] = explode(",", $lulwut[17]);
   				$lol['th'] = explode(",", $lulwut[18]);
   				$lol['sl'] = explode(",", $lulwut[19]);
   				$lol['frm'] = explode(",", $lulwut[20]);
   				$lol['rc'] = explode(",", $lulwut[21]);
   				$lol['hun'] = explode(",", $lulwut[22]);
   				$lol['cs'] = explode(",", $lulwut[23]);
   				$lol['sum'] = explode(",", $lulwut[24]);
   				$lol['dun'] = explode(",", $lulwut[25]);

				$lol['hp'] = str_replace('-1', '10', $lol['hp']);
				$lol['att'] = str_replace('-1', '1', $lol['att']);
				$lol['def'] = str_replace('-1', '1', $lol['def']);
				$lol['str'] = str_replace('-1', '1', $lol['str']);
				$lol['rng'] = str_replace('-1', '1', $lol['rng']);
				$lol['pry'] = str_replace('-1', '1', $lol['pry']);
				$lol['mag'] = str_replace('-1', '1', $lol['mag']);
				$lol['ck'] = str_replace('-1', '1', $lol['ck']);
				$lol['wc'] = str_replace('-1', '1', $lol['wc']);
				$lol['flt'] = str_replace('-1', '1', $lol['flt']);
				$lol['fsh'] = str_replace('-1', '1', $lol['fsh']);
				$lol['fm'] = str_replace('-1', '1', $lol['fm']);
				$lol['cra'] = str_replace('-1', '1', $lol['cra']);
				$lol['smi'] = str_replace('-1', '1', $lol['smi']);
				$lol['min'] = str_replace('-1', '1', $lol['min']);
				$lol['her'] = str_replace('-1', '1', $lol['her']);
				$lol['ag'] = str_replace('-1', '1', $lol['ag']);
				$lol['th'] = str_replace('-1', '1', $lol['th']);
				$lol['sl'] = str_replace('-1', '1', $lol['sl']);
				$lol['frm'] = str_replace('-1', '1', $lol['frm']);
				$lol['rc'] = str_replace('-1', '1', $lol['rc']);
				$lol['hun'] = str_replace('-1', '1', $lol['hun']);
				$lol['cs'] = str_replace('-1', '1', $lol['cs']);
				$lol['sum'] = str_replace('-1', '1', $lol['sum']);
				$lol['dun'] = str_replace('-1', '1', $lol['dun']);


				$hiscores = $db->query("UPDATE `clan_members` SET 
						`attack` 	= '".$lol['att'][1]."',	
						`strength` 	= '".$lol['str'][1]."',	
						`defence` 	= '".$lol['def'][1]."',
						`ranged` 	= '".$lol['rng'][1]."',
						`prayer`	= '".$lol['pry'][1]."',
						`magic` 	= '".$lol['mag'][1]."',
						`runecrafting` 	= '".$lol['rc'][1]."',
						`construction` 	= '".$lol['cs'][1]."',
						`constitution` 	= '".$lol['hp'][1]."',
						`agility` 	= '".$lol['ag'][1]."',
						`herblore` 	= '".$lol['her'][1]."',
						`thieving` 	= '".$lol['th'][1]."',
						`crafting` 	= '".$lol['cra'][1]."',
						`fletching` 	= '".$lol['flt'][1]."',
						`slayer` 	= '".$lol['sl'][1]."',
						`hunter` 	= '".$lol['hun'][1]."',
						`mining` 	= '".$lol['min'][1]."',
						`smithing` 	= '".$lol['smi'][1]."',
						`fishing` 	= '".$lol['fsh'][1]."',
						`cooking` 	= '".$lol['ck'][1]."',
						`firemaking` 	= '".$lol['att'][1]."',
						`woodcutting` 	= '".$lol['wc'][1]."',
						`farming` 	= '".$lol['frm'][1]."',
						`summoning` 	= '".$lol['sum'][1]."',
						`dungeoneering` = '".$lol['dun'][1]."'
					WHERE `username` = '$username'
				") or die(mysql_error());


		}

 

That's my code, it grabs the stats from an external source, then replaces all -1's with 1. Then inputs it in the database. I'm wondering if there's a way in which it could be shortened.

 

Also, thanks for removing my warn.

Link to comment
https://forums.phpfreaks.com/topic/201495-better-method/#findComment-1057156
Share on other sites

Do what I suggested for the str_replaces. I should be more strict with you over MSN.

If it works, it will save like 20 lines. You could PERHAPS use a similar method with the explodes.

 

Variable variables are very inefficient. Take a look at array_map to take care of the calls to str_replace. You should also look into using implode to build your database query.

 

Id do it for you but that's not much fun.

Link to comment
https://forums.phpfreaks.com/topic/201495-better-method/#findComment-1057169
Share on other sites

I would also make a master array where the keys are the offset in the $lulwut array and the values are the corresponding column names so that you can dynamically produce the query from the correct data by using a foreach(){} loop to iterate over the master array to get the key/values to process the data.

Link to comment
https://forums.phpfreaks.com/topic/201495-better-method/#findComment-1057179
Share on other sites

You should also look into using implode() to build your database query.

You could do this.. for the queries maybe.

<?php
$array1 = array('attack', 'defence', 'magic'); //Names
$array2 = array('attvalue', 'defvalue', 'magvalue'); //Values
$newarray = array($array1 => $array2); //Make an associative array eg: $newarray['attack'] = 'attvalue'
foreach($newarray as $key => $value) {
    $sets[] = '`' . $key . '` = \'' . $value . '\''; //This is like.. `attack` = 'attvalue' in a query
}
$sets = implode(', ' $sets); //Implode it so it's like `attack` = 'attvalue', `defence` = 'defvalue' - in a query
$query = $db->query("update `table` set " . $sets . " where `username` = '$username'"); //Then the query.
?>

 

There is probs a better way, but yes. I don't understand array_map very well either. Just have a read on it. http://uk.php.net/array_map

Link to comment
https://forums.phpfreaks.com/topic/201495-better-method/#findComment-1057192
Share on other sites

Thorpe. I had a read of array map. Would it be right in karls case if he did this..

 

function Str_Replace_This($value) {
           $value = str_replace('-1', '1', $value);
}
$array = array_map(Str_Replace_This, $lol);

So $array holds the new values. So if $lol['att'] went in first, $array[1] will be thev alue of $lol['att'] after str_replace.

 

and putting it all together.. would make

<?php
function Str_Replace_This($value) {
           $value = str_replace('-1', '1', $value);
}

$array1 = array('attack', 'defence', 'magic'); // The Names

$array = array_map(Str_Replace_This, $lol); // The values.

$newarray = array($array1 => $array2); //Make an associative array eg: $newarray['attack'] = 'attack value'

foreach($newarray as $key => $value) {
    $sets[] = '`' . $key . '` = \'' . $value . '\''; //This is like.. `attack` = 'attack value' in a query
}
$sets = implode(', ' $sets); //Implode it so it's like `attack` = 'attack value', `defence` = 'defence value' - in a query
$query = $db->query("update `table` set " . $sets . " where `username` = '$username'"); //Then the query.
?>

 

Although with this, you got to be careful. If for instance.. $lol['att'] is not first into the array, it will just mess up pretty much. So you got to order them.. properly. If you get it working with 3 results.

attack

defence

magic, in that order, even through the explodes. See if that works.. then you build upon it in a specific order. Changing the order of the names, based on the order of your explodes.

Link to comment
https://forums.phpfreaks.com/topic/201495-better-method/#findComment-1057202
Share on other sites

Or you could just do this -

<?php
$arr = array();
$arr[1] = 'attack'; // $lol['att'] = explode(",", $lulwut[1]);
$arr[3] = 'strength'; // $lol['str'] = explode(",", $lulwut[3]);
$arr[2] = 'defence'; // $lol['def'] = explode(",", $lulwut[2]);
$arr[5] = 'ranged'; // $lol['rng'] = explode(",", $lulwut[5]);
$arr[6] = 'prayer'; // $lol['pry'] = explode(",", $lulwut[6]);
$arr[7] = 'magic'; // $lol['mag'] = explode(",", $lulwut[7]);
$arr[21] = 'runecrafting'; // $lol['rc'] = explode(",", $lulwut[21]);
$arr[23] = 'construction'; // $lol['cs'] = explode(",", $lulwut[23]);
$arr[4] = 'constitution'; // $lol['hp'] = explode(",", $lulwut[4]);
$arr[17] = 'agility'; // $lol['ag'] = explode(",", $lulwut[17]);
$arr[16] = 'herblore'; // $lol['her'] = explode(",", $lulwut[16]);
$arr[18] = 'thieving'; // $lol['th'] = explode(",", $lulwut[18]);
$arr[13] = 'crafting'; // $lol['cra'] = explode(",", $lulwut[13]);
$arr[10] = 'fletching'; // $lol['flt'] = explode(",", $lulwut[10]);
$arr[19] = 'slayer'; // $lol['sl'] = explode(",", $lulwut[19]);
$arr[22] = 'hunter'; // $lol['hun'] = explode(",", $lulwut[22]);
$arr[15] = 'mining'; // $lol['min'] = explode(",", $lulwut[15]);
$arr[14] = 'smithing'; // $lol['smi'] = explode(",", $lulwut[14]);
$arr[11] = 'fishing'; // $lol['fsh'] = explode(",", $lulwut[11]);
$arr[8] = 'cooking'; // $lol['ck'] = explode(",", $lulwut[8]);
$arr[12] = 'firemaking'; // $lol['fm'] = explode(",", $lulwut[12]);
$arr[9] = 'woodcutting'; // $lol['wc'] = explode(",", $lulwut[9]);
$arr[20] = 'farming'; // $lol['frm'] = explode(",", $lulwut[20]);
$arr[24] = 'summoning'; // $lol['sum'] = explode(",", $lulwut[24]);
$arr[25] = 'dungeoneering'; //$lol['dun'] = explode(",", $lulwut[25]);

$index = file_get_contents('http://hiscore.runescape.com/index_lite.ws?player=' . $username);
if ($index != ""){
$lulwut = explode("\n", $index);
$overall = explode(",", $lulwut[0]); // not used
$query = "UPDATE `clan_members` SET ";
foreach($arr as $key => $name){
	$temp = explode(",", $lulwut[$key]); // $lol['att'] = explode(",", $lulwut[1]);
	$temp = str_replace('-1', '1', $temp); // $lol['att'] = str_replace('-1', '1', $lol['att']);
	$query .= "`$name` = '{$temp[1]}',";
}
$query = rtrim($query,',');
$query .= " WHERE `username` = '$username'";
echo $query;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/201495-better-method/#findComment-1057213
Share on other sites

Or you could just do this -

<?php
$arr = array();
$arr[1] = 'attack'; // $lol['att'] = explode(",", $lulwut[1]);
$arr[3] = 'strength'; // $lol['str'] = explode(",", $lulwut[3]);
$arr[2] = 'defence'; // $lol['def'] = explode(",", $lulwut[2]);
$arr[5] = 'ranged'; // $lol['rng'] = explode(",", $lulwut[5]);
$arr[6] = 'prayer'; // $lol['pry'] = explode(",", $lulwut[6]);
$arr[7] = 'magic'; // $lol['mag'] = explode(",", $lulwut[7]);
$arr[21] = 'runecrafting'; // $lol['rc'] = explode(",", $lulwut[21]);
$arr[23] = 'construction'; // $lol['cs'] = explode(",", $lulwut[23]);
$arr[4] = 'constitution'; // $lol['hp'] = explode(",", $lulwut[4]);
$arr[17] = 'agility'; // $lol['ag'] = explode(",", $lulwut[17]);
$arr[16] = 'herblore'; // $lol['her'] = explode(",", $lulwut[16]);
$arr[18] = 'thieving'; // $lol['th'] = explode(",", $lulwut[18]);
$arr[13] = 'crafting'; // $lol['cra'] = explode(",", $lulwut[13]);
$arr[10] = 'fletching'; // $lol['flt'] = explode(",", $lulwut[10]);
$arr[19] = 'slayer'; // $lol['sl'] = explode(",", $lulwut[19]);
$arr[22] = 'hunter'; // $lol['hun'] = explode(",", $lulwut[22]);
$arr[15] = 'mining'; // $lol['min'] = explode(",", $lulwut[15]);
$arr[14] = 'smithing'; // $lol['smi'] = explode(",", $lulwut[14]);
$arr[11] = 'fishing'; // $lol['fsh'] = explode(",", $lulwut[11]);
$arr[8] = 'cooking'; // $lol['ck'] = explode(",", $lulwut[8]);
$arr[12] = 'firemaking'; // $lol['fm'] = explode(",", $lulwut[12]);
$arr[9] = 'woodcutting'; // $lol['wc'] = explode(",", $lulwut[9]);
$arr[20] = 'farming'; // $lol['frm'] = explode(",", $lulwut[20]);
$arr[24] = 'summoning'; // $lol['sum'] = explode(",", $lulwut[24]);
$arr[25] = 'dungeoneering'; //$lol['dun'] = explode(",", $lulwut[25]);

$index = file_get_contents('http://hiscore.runescape.com/index_lite.ws?player=' . $username);
if ($index != ""){
$lulwut = explode("\n", $index);
$overall = explode(",", $lulwut[0]); // not used
$query = "UPDATE `clan_members` SET ";
foreach($arr as $key => $name){
	$temp = explode(",", $lulwut[$key]); // $lol['att'] = explode(",", $lulwut[1]);
	$temp = str_replace('-1', '1', $temp); // $lol['att'] = str_replace('-1', '1', $lol['att']);
	$query .= "`$name` = '{$temp[1]}',";
}
$query = rtrim($query,',');
$query .= " WHERE `username` = '$username'";
echo $query;
}
?>

 

That works fine, thanks a lot PFMaBiSmAd.

Link to comment
https://forums.phpfreaks.com/topic/201495-better-method/#findComment-1057217
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.