15, October 2008

update all results in a table - webmaster forum

 
Webdigity webmaster forums
This forum shares its ad revenue with its members!
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: update all results in a table
« previous next »
Pages: [1] 2 Print

Author Topic: update all results in a table  (Read 1683 times)
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 884
1636 credits
Members referred : 4



« on: Sep 12, 2006, 08:46:55 PM »

ok so what im doing THIS time is i have it set so you are able to update a persons data by clicking a link, update. this is ok, and it works fine except when we have 30+ users it can be a hassle to go through each one and click update, so i want to make a master update link that can update ALL results instead just the one you chose, here is the code I use to update a persons data via the link, commands.php?cmd=update&player_id=(some#). i simply want to add a new function to this commands page, update_all and in this case it wouldnt use a particular player ID to do the update, it selects all player names (player_name WHERE player_id = all?) from the table and then updates all of them (and i know this will be a big page most likely and take a while to parse/execute, thats understandable).

Code:
<?php
//unrelated, if statement for edit has been omitted from this snippet
}
elseif(
$_GET["cmd"]=="update")
{
opentable($module_name." v".$version." - Update Player");
$result dbquery("SELECT * from ".$db_prefix."hiscores where player_id = '$_GET[player_id]'"); 
$data dbarray($result); 
$player_id $data['player_id'];
$player_name $data['player_name'];
require_once "get_stats.php";
require_once "combat_formula.php";
$update_player = ("UPDATE ".$db_prefix."hiscores set player_att = '$attack', player_str = '$strength', 
player_def = '$defence', player_hp = '$hitpoints', player_range = '$ranged', player_mage = '$magic', 
player_pray = '$player_prayer', player_mining = '$mining', player_smith = '$smithing', player_wc = '$woodcutting', 
player_fm = '$farming', player_agil = '$agility', player_herb = '$herblore', 
player_fish = '$fishing', player_thief = '$thieving', player_cook = '$cooking', 
player_craft = '$crafting', player_fletch = '$fletching', player_slay = '$slayer', 
player_farm = '$farming', player_con = '$construction', player_overall = '$overall',
player_cmb = '$combat_level' WHERE player_id = '$player_id'"
);
$run_query dbquery($update_player);
if (!$run_query){ 
echo "<center>Error Updating Player Stats.</center>"
} else {
echo "<center>The player <b>".$data['player_name']."</b> has been successfully updated.";
echo("<div align='center'><table><tr><td valign='top'>Player Name: </td><td valign='top'>".$data['player_name']."</td></tr>
<tr><td valign='top'>Account ID: </td><td valign='top'>"
.$data['player_user']."</td></tr>
<tr><td valign='top'>Attack: </td><td valign='top'>"
.$attack."</td></tr>
<tr><td valign='top'>Strength: </td><td valign='top'>"
.$strength."</td></tr>
<tr><td valign='top'>Defense: </td><td valign='top'>"
.$defence."</td></tr>
<tr><td valign='top'>Hit Points: </td><td valign='top'>"
.$hitpoints."</td></tr>
<tr><td valign='top'>Prayer: </td><td valign='top'>"
.$player_prayer."</td></tr>
<tr><td valign='top'>Range: </td><td valign='top'>"
.$ranged."</td></tr>
<tr><td valign='top'>Magic: </td><td valign='top'>"
.$magic."</td></tr>
<tr><td valign='top'>Mining: </td><td valign='top'>"
.$mining."</td></tr>
<tr><td valign='top'>Smithing: </td><td valign='top'>"
.$smithing."</td></tr>
<tr><td valign='top'>Woodcutting: </td><td valign='top'>"
.$woodcutting."</td></tr>
<tr><td valign='top'>Firemaking: </td><td valign='top'>"
.$firemaking."</td></tr>
<tr><td valign='top'>Agility: </td><td valign='top'>"
.$agility."</td></tr>
<tr><td valign='top'>Herblore: </td><td valign='top'>"
.$herblore."</td></tr>
<tr><td valign='top'>Fishing: </td><td valign='top'>"
.$fishing."</td></tr>
<tr><td valign='top'>Thieving: </td><td valign='top'>"
.$thieving."</td></tr>
<tr><td valign='top'>Cooking: </td><td valign='top'>"
.$cooking."</td></tr>
<tr><td valign='top'>Crafting: </td><td valign='top'>"
.$crafting."</td></tr>
<tr><td valign='top'>Fletching: </td><td valign='top'>"
.$fletching."</td></tr>
<tr><td valign='top'>Slayer: </td><td valign='top'>"
.$slayer."</td></tr>
<tr><td valign='top'>Farming: </td><td valign='top'>"
.$farming."</td></tr>
<tr><td valign='top'>Construction: </td><td valign='top'>"
.$construction."</td></tr>
<tr><td valign='top'>Overall: </td><td valign='top'>"
.$overall."</td></tr>
<tr><td valign='top'>Combat Stats: </td><td valign='top'>Combat Level: $combat_level<br>
Combat Class: $class</td></tr></table></div>"
);
echo "<br><br>Click [<a href='".BASEDIR."administration/hiscores/index.php".$aidlink."'>here</a>] to return to the ".$module_name."</center>"
}
closetable();

elseif(
$_GET["cmd"]=="update_all"
{
opentable($module_name." v".$version." - Update All Players");
//this is where I want to perform the update of ALL members
closetable();
}
?>

« Last Edit: Sep 12, 2006, 08:48:26 PM by Meth0d »

Visit through proxy Visit through proxy Visit through proxy

Last blog : phpHaze 1.59.1 in Development
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8132
41749 credits
Members referred : 3



« Reply #1 on: Sep 12, 2006, 10:58:14 PM »

as you have it ready for one, I guess doing it for more than one is an easy process.

Just hold the player ids in one array and then use a function like foreach Visit through proxy to loop it Wink

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 884
1636 credits
Members referred : 4



« Reply #2 on: Sep 13, 2006, 12:55:24 AM »

ok so how would I get all the values for player_id's from the table and then load all of those into the array, first of all ? and i have no experience with foreach Shocked

Visit through proxy Visit through proxy Visit through proxy

Last blog : phpHaze 1.59.1 in Development
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6357
38966 credits
Members referred : 374


It's time to use PHP5!


« Reply #3 on: Sep 13, 2006, 01:26:36 AM »



Last blog : Upload images for usage in TinyMCE
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 884
1636 credits
Members referred : 4



« Reply #4 on: Sep 13, 2006, 03:28:23 AM »

ok i get this error
Quote
Warning: Invalid argument supplied for foreach() in /hsphere/local/home/meth0d42/3rd-evolution.net/administration/hiscores/commands.php on line 92

when using this code
Code:
<?php
opentable
($module_name." v".$version." - Update All Players");
$result dbquery("SELECT player_id FROM ".$db_prefix."hiscores");
$data dbarray($result);
foreach ($data['player_id'] as $value)
{
require_once "get_stats.php";
require_once "combat_formula.php";
$update_player = ("UPDATE ".$db_prefix."hiscores set player_att = '$attack', player_str = '$strength', 
player_def = '$defence', player_hp = '$hitpoints', player_range = '$ranged', player_mage = '$magic', 
player_pray = '$player_prayer', player_mining = '$mining', player_smith = '$smithing', player_wc = '$woodcutting', 
player_fm = '$farming', player_agil = '$agility', player_herb = '$herblore', 
player_fish = '$fishing', player_thief = '$thieving', player_cook = '$cooking', 
player_craft = '$crafting', player_fletch = '$fletching', player_slay = '$slayer', 
player_farm = '$farming', player_con = '$construction', player_overall = '$overall',
player_cmb = '$combat_level' WHERE player_id = $value"
);
$run_query dbquery($update_player);
if (!$run_query){ 
echo "<center>Error Updating Player Stats.</center>"
} else {
echo "<center>All players successfully updated.</center>";
}
?>

line 92 that it is referring to is this line:
Code:
<?php foreach ($data['player_id'] as $value?>
« Last Edit: Sep 13, 2006, 03:30:37 AM by Meth0d »

Visit through proxy Visit through proxy Visit through proxy

Last blog : phpHaze 1.59.1 in Development
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6357
38966 credits
Members referred : 374


It's time to use PHP5!


« Reply #5 on: Sep 13, 2006, 07:01:54 AM »

from the manual:

Quote
This simply gives an easy way to iterate over arrays. foreach works only on arrays, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable.

player id is not an array


Last blog : Upload images for usage in TinyMCE
WebDigity Gangsta
***
Gender: Male
Posts: 105
18 credits
Members referred : 0



« Reply #6 on: Sep 13, 2006, 10:04:07 AM »

I take it thats for a runescape highscore?
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8132
41749 credits
Members referred : 3



« Reply #7 on: Sep 13, 2006, 10:23:47 AM »

You forgot to load the player ids in the $data array Smiley

while( $tmp = mysql_fetch_array($result) )
   $data[] = $tmp ['player_id'];

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 884
1636 credits
Members referred : 4



« Reply #8 on: Sep 13, 2006, 03:23:38 PM »

You forgot to load the player ids in the $data array Smiley

while( $tmp = mysql_fetch_array($result) )
   $data[] = $tmp ['player_id'];

where should i add these lines of code and what do I change in the for each line?

I take it thats for a runescape highscore?

yes
« Last Edit: Sep 13, 2006, 03:29:46 PM by Meth0d »

Visit through proxy Visit through proxy Visit through proxy

Last blog : phpHaze 1.59.1 in Development
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8132
41749 credits
Members referred : 3



« Reply #9 on: Sep 13, 2006, 03:50:39 PM »

You should place it after the line :

$data = dbarray($result);

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 884
1636 credits
Members referred : 4



« Reply #10 on: Sep 13, 2006, 04:12:18 PM »

im getting similair error still

Code:
<?php

elseif(
$_GET["cmd"]=="update_all"
{
opentable($module_name." v".$version." - Update All Players");
$result dbquery("SELECT player_id, player_name FROM ".$db_prefix."hiscores");
$data dbarray($result);
while( $tmp mysql_fetch_array($result) )
   
$data[] = $tmp ['player_id'];
foreach ($data['player_id'] as $value)
{
require_once "get_stats.php";
require_once "combat_formula.php";
$update_player = ("UPDATE ".$db_prefix."hiscores set player_att = '$attack', player_str = '$strength', 
player_def = '$defence', player_hp = '$hitpoints', player_range = '$ranged', player_mage = '$magic', 
player_pray = '$player_prayer', player_mining = '$mining', player_smith = '$smithing', player_wc = '$woodcutting', 
player_fm = '$farming', player_agil = '$agility', player_herb = '$herblore', 
player_fish = '$fishing', player_thief = '$thieving', player_cook = '$cooking', 
player_craft = '$crafting', player_fletch = '$fletching', player_slay = '$slayer', 
player_farm = '$farming', player_con = '$construction', player_overall = '$overall',
player_cmb = '$combat_level' WHERE player_id = $value"
);
$run_query dbquery($update_player);
if (!$run_query){ 
echo "<center>Error Updating Player Stats.</center>"
} else {
echo "<center>Player: <b>".$data['player_name']."</b> successfully updated.</center>";
}


echo "<center><br><br>Click [<a href='".BASEDIR."administration/hiscores/index.php".$aidlink."'>here</a>] to return to the ".$module_name."</center>"
closetable();
}
?>

Quote
Warning: Invalid argument supplied for foreach() in /hsphere/local/home/meth0d42/3rd-evolution.net/administration/hiscores/commands.php on line 94

Visit through proxy Visit through proxy Visit through proxy

Last blog : phpHaze 1.59.1 in Development
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8132
41749 credits
Members referred : 3



« Reply #11 on: Sep 13, 2006, 04:40:30 PM »

I think you need to learn the basics before trying to modify a script method....

Change this :

foreach ($data['player_id'] as $value)

to this :

foreach ($data as $value)

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 884
1636 credits
Members referred : 4



« Reply #12 on: Sep 13, 2006, 04:51:14 PM »

for some reason this is taking the first player id result and updating its values (say it was player id 2 was the first result, it took his data and applied it to every result.. instead of loading each result then updating it with its correct data)

Code:
<?php

elseif(
$_GET["cmd"]=="update_all"
{
opentable($module_name." v".$version." - Update All Players");
$result dbquery("SELECT player_id, player_name FROM ".$db_prefix."hiscores");
$data dbarray($result);
while( $tmp mysql_fetch_array($result) )
    $data[] = $tmp ['player_id'];
foreach ($data as $value)
{
require_once "get_stats.php";
require_once "combat_formula.php";
$update_player = ("UPDATE ".$db_prefix."hiscores set player_att = '$attack', player_str = '$strength', 
player_def = '$defence', player_hp = '$hitpoints', player_range = '$ranged', player_mage = '$magic', 
player_pray = '$player_prayer', player_mining = '$mining', player_smith = '$smithing', player_wc = '$woodcutting', 
player_fm = '$farming', player_agil = '$agility', player_herb = '$herblore', 
player_fish = '$fishing', player_thief = '$thieving', player_cook = '$cooking', 
player_craft = '$crafting', player_fletch = '$fletching', player_slay = '$slayer', 
player_farm = '$farming', player_con = '$construction', player_overall = '$overall',
player_cmb = '$combat_level' WHERE player_id = $value"
);
$run_query dbquery($update_player);
if (!$run_query){ 
echo "<center>Error Updating Player Stats.</center>"
} else {
echo "<center>Player: <b>".$data['player_name']."</b> successfully updated.</center>";
}


echo "<center><br><br>Click [<a href='".BASEDIR."administration/hiscores/index.php".$aidlink."'>here</a>] to return to the ".$module_name."</center>"
closetable();
}

?>


output (parsed result)
Quote
Player: XxIcEyxX successfully updated.
Unknown column 'XxIcEyxX' in 'where clause'
Error Updating Player Stats.
Player: XxIcEyxX successfully updated.
Player: XxIcEyxX successfully updated.
Player: XxIcEyxX successfully updated.
Player: XxIcEyxX successfully updated.
Player: XxIcEyxX successfully updated.
Player: XxIcEyxX successfully updated.
Player: XxIcEyxX successfully updated.
Player: XxIcEyxX successfully updated.
Player: XxIcEyxX successfully updated.
Player: XxIcEyxX successfully updated.
Player: XxIcEyxX successfully updated.
Player: XxIcEyxX successfully updated.
Player: XxIcEyxX successfully updated.
Player: XxIcEyxX successfully updated.
Player: XxIcEyxX successfully updated.
Player: XxIcEyxX successfully updated.
Player: XxIcEyxX successfully updated.
Player: XxIcEyxX successfully updated.
Player: XxIcEyxX successfully updated.
Player: XxIcEyxX successfully updated.
Player: XxIcEyxX successfully updated.

Visit through proxy Visit through proxy Visit through proxy

Last blog : phpHaze 1.59.1 in Development
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8132
41749 credits
Members referred : 3



« Reply #13 on: Sep 13, 2006, 04:53:19 PM »

BTW you should delete that line :

$data = dbarray($result);

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 884
1636 credits
Members referred : 4



« Reply #14 on: Sep 13, 2006, 04:57:28 PM »

i tried that before, but it was giving me the same result it is giving me now, its finding (for some odd reason) the last player to register an account on the site itself when we arent even using that table, we are using a different player table

Quote
Player: successfully updated.
Unknown column 'lewatha' in 'where clause'
Error Updating Player Stats.
Player: successfully updated.
Player: successfully updated.
Player: successfully updated.
Player: successfully updated.
Player: successfully updated.
Player: successfully updated.
Player: successfully updated.
Player: successfully updated.
Player: successfully updated.
Player: successfully updated.
Player: successfully updated.
Player: successfully updated.
Player: successfully updated.
Player: successfully updated.
Player: successfully updated.
Player: successfully updated.
Player: successfully updated.
Player: successfully updated.
Player: successfully updated.
Player: successfully updated.
Player: successfully updated.
Player: successfully updated.

Visit through proxy Visit through proxy Visit through proxy

Last blog : phpHaze 1.59.1 in Development
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8132
41749 credits
Members referred : 3



« Reply #15 on: Sep 13, 2006, 05:02:07 PM »

Did you removed the :

$data = dbarray($result);

?

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 884
1636 credits
Members referred : 4



« Reply #16 on: Sep 13, 2006, 05:03:21 PM »

yes that is why the result changed from all "xxiceyxx" to simply not updating anyone (in actuality, it resets all accounts data)

Visit through proxy Visit through proxy Visit through proxy

Last blog : phpHaze 1.59.1 in Development