seany123 Posted June 1, 2009 Share Posted June 1, 2009 how would i ouput these echo's instead of echos. <?php $energyreduce = ($player->energy); //UPDATE STRENGTH. if ($_POST['full_strength']) { if($player->energy + $energy1 < 1 ) { echo"<br>You dont have any energy left"; echo"<br><br><a href=\"gym.php\">Back</a>"; exit; } $query = $db->execute("update `players` set `energy`=?, `awake`=?, `strength`=?, `total`=? where `id`=?", array($player->energy - $player->energy, ($player->awake - $awakeloss) < 0 ? 0 : $player->awake - $awakeloss, $player->strength + $statgain, $player->total + $statgain, $player->id )); echo "<br>You gained " . number_format($statgain, 2, ".", ",") . " Strength"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/160551-outputting-instead-of-echoing/ Share on other sites More sharing options...
Alex Posted June 1, 2009 Share Posted June 1, 2009 Echoing is outputting? Do you mean making it only output a single string? If so then you can just build it all into one string. Instead of echoing something just add it a collective output string, that you'll echo at the end. $string .= "stuff"; $string .= ", more stuff.."; echo $string; Output: stuff, more stuff.. Quote Link to comment https://forums.phpfreaks.com/topic/160551-outputting-instead-of-echoing/#findComment-847315 Share on other sites More sharing options...
seany123 Posted June 1, 2009 Author Share Posted June 1, 2009 well for example after that code i have this.. <form method='post'><br> <table width='80%' align='center'> <tr> <td align='center'><input type='submit' name='full_strength' value='All Strength'></td> <td align='center'><input type='submit' name='full_defense' value='All Defense'></td> <td align='center'><input type='submit' name='full_speed' value='All Speed'></td> <td rowspan='2' width='10'> </td> <td rowspan='2' width='100'> <b>Refill:</b><br><br> <a href='refillenergy2.php'> Energy</a><br><br> <a href='refillawake2.php'> Awake</a><br> </td> </tr> <tr> <td align='center'> </td> <td align='center'><input type='submit' name='train' value='Train'></td> <td align='center'> </td> </tr> </table> </form> </tr> </table></div></div> <div class="g_content"><h3 style="text-align: left"> Attributes</h3><div class="g_text"> <table width='100%'> <tr> <td width='15%'>Strength:</td> <td width='35%'><?= $english_format_number = number_format($player->strength);?></td> <td width='15%'>Defense:</td> <td width='35%'><?= $english_format_number = number_format($player->defense);?></td> </tr> <tr> <td width='15%'>Speed:</td> <td width='35%'><?= $english_format_number = number_format($player->speed);?></td> <td width='15%'>Total:</td> <td width='35%'><?= $english_format_number = number_format($player->total);?></td> </tr> </table></div></div> the echo (output) shows the amount a value in the database has been added.... the following shows a value in the database.. <?= $english_format_number = number_format($player->total);?> however when i run the form and get the echo/output i have to refresh the page before the a $player->total value is updated. Quote Link to comment https://forums.phpfreaks.com/topic/160551-outputting-instead-of-echoing/#findComment-847322 Share on other sites More sharing options...
Alex Posted June 1, 2009 Share Posted June 1, 2009 Isn't that because after you submit the form you're outputting the display variable before you update it? Quote Link to comment https://forums.phpfreaks.com/topic/160551-outputting-instead-of-echoing/#findComment-847324 Share on other sites More sharing options...
seany123 Posted June 1, 2009 Author Share Posted June 1, 2009 no, i cant see how thats happening. here is the entire page.. <?php include("lib.php"); define("PAGENAME", "Gym"); $player = check_user($secret_key, $db); //UPDATE STRENGTH. if ($_POST['full_strength']) { if($player->energy + $energy1 < 1 ) { $output .= "<br>You dont have any energy left"; $output .= "<br><br><a href=\"gym3.php\">Back</a>"; exit; } $query = $db->execute("update `players` set `energy`=?, `awake`=?, `strength`=?, `total`=? where `id`=?", array($player->energy - $player->energy, ($player->awake - $awakeloss) < 0 ? 0 : $player->awake - $awakeloss, $player->strength + $statgain, $player->total + $statgain, $player->id )); $output .= "<br>You gained " . number_format($statgain, 2, ".", ",") . " Strength"; } $energyreduce = ($player->energy); //UPDATE DEFENSE. if ($_POST['full_defense']) { if($player->energy + $energy1 < 1 ) { $output .= "<br>You dont have any energy left"; $output .= "<br><br><a href=\"gym3.php\">Back</a>"; exit; } $query = $db->execute("update `players` set `energy`=?, `awake`=?, `defense`=?, `total`=? where `id`=?", array($player->energy - $player->energy, ($player->awake - $awakeloss) < 0 ? 0 : $player->awake - $awakeloss, $player->defense + $statgain, $player->total + $statgain, $player->id )); $output .= "<br>You gained " . number_format($statgain, 2, ".", ",") . " Defense"; } $energyreduce = ($player['energy']); // UPDATE SPEED. if ($_POST['full_speed']) { if($player->energy + $energy1 < 1 ) { $output .= "<br>You dont have any energy left"; $output .= "<br><br><a href=\"gym3.php\">Back</a>"; exit; } $query = $db->execute("update `players` set `energy`=?, `awake`=?, `speed`=?, `total`=? where `id`=?", array($player->energy - $player->energy, ($player->awake - $awakeloss) < 0 ? 0 : $player->awake - $awakeloss, $player->speed + $statgain, $player->total + $statgain, $player->id )); $output .= "<br>You gained " . number_format($statgain, 2, ".", ",") . " Speed"; } if ($_POST['energy_str'] + $_POST['energy_def'] + $_POST['energy_spd'] > $player->energy) { $output .= "<br>You dont have any energy left"; $output .= "<br><br><a href=\"gym3.php\">Back</a>"; exit; } if ($_POST['energy_str']) { if($player->energy < 1 || $_POST['energy_str'] > $player->energy) { $output .= "<br>You dont have any energy left"; $output .= "<br><br><a href=\"gym3.php\">Back</a>"; exit; } else if ($_POST['energy_str'] + $_POST['energy_def'] + $_POST['energy_spd'] <= $player->energy) { $energyloss = ($_POST['energy_str'] + $_POST['energy_def'] + $_POST['energy_spd']); $query = $db->execute("update `players` set `energy`=?, `awake`=?, `strength`=?, `total`=? where `id`=?", array($player->energy - $energyloss, ($player->awake - $awakeloss2) < 0 ? 0 : $player->awake - $awakeloss2, $player->strength + $statgain2, $player->total + $statgain5, $player->id )); $output .= "<br>You trained\n" . $_POST['energy_str'] . "\ntimes and You gained " . number_format($statgain2, 2, ".", ",") . " Strength<br>"; } } if ($_POST['energy_def']) { if($player->energy < 1 || $_POST['energy_def'] > $player->energy) { $output .= "<br>You dont have any energy left"; $output .= "<br><br><a href=\"gym3.php\">Back</a>"; exit; } else if ($_POST['energy_str'] + $_POST['energy_def'] + $_POST['energy_spd'] <= $player->energy) { $energyloss = ($_POST['energy_str'] + $_POST['energy_def'] + $_POST['energy_spd']); $query = $db->execute("update `players` set `energy`=?, `awake`=?, `defense`=?, `total`=? where `id`=?", array($player->energy - $energyloss, ($player->awake - $awakeloss3) < 0 ? 0 : $player->awake - $awakeloss3, $player->defense + $statgain3, $player->total + $statgain5, $player->id )); $output .= "<br>You trained\n" . $_POST['energy_def'] . "\ntimes and You gained " . number_format($statgain3, 2, ".", ",") . " Defense<br>" ; } } if ($_POST['energy_spd']) { if($player->energy < 1 || $_POST['energy_spd'] > $player->energy) { $output .= "<br>You dont have any energy left"; $output .= "<br><br><a href=\"gym3.php\">Back</a>"; exit; } else if ($_POST['energy_str'] + $_POST['energy_def'] + $_POST['energy_spd'] <= $player->energy) { $energyloss = ($_POST['energy_str'] + $_POST['energy_def'] + $_POST['energy_spd']); $query = $db->execute("update `players` set `energy`=?, `awake`=?, `speed`=?, `total`=? where `id`=?", array($player->energy - $energyloss, ($player->awake - $awakeloss4) < 0 ? 0 : $player->awake - $awakeloss4, $player->speed + $statgain4, $player->total + $statgain5, $player->id )); $output .= "<br>You trained\n" . $_POST['energy_spd'] . "\ntimes and You gained " . number_format($statgain4, 2, ".", ",") . " Speed<br>" ; } } include("templates/private_header3.php"); ?> <html> <head> <title>MafiaKiller</title> <link rel="stylesheet" href="/css/style.css" type="text/css" media="all" /> </head> <body alink="#cc9900" vlink="#cc9900" link="#cc9900"> </div> </div> <div id="left_c"><div class="g_content"><h3 style="text-align: left"> Gym</h3><div class="g_text"> <table width='100%'> <tr> <?php echo $output; ?> <form method='post'><br> <table width='80%' align='center'> <tr> <td align='center'><input type='submit' name='full_strength' value='All Strength'></td> <td align='center'><input type='submit' name='full_defense' value='All Defense'></td> <td align='center'><input type='submit' name='full_speed' value='All Speed'></td> <td rowspan='2' width='10'> </td> <td rowspan='2' width='100'> <b>Refill:</b><br><br> <a href='refillenergy2.php'> Energy</a><br><br> <a href='refillawake2.php'> Awake</a><br> </td> </tr> <tr> <td align='center'> </td> <td align='center'><input type='submit' name='train' value='Train'></td> <td align='center'> </td> </tr> </table> </form> </tr> </table></div></div> <div class="g_content"><h3 style="text-align: left"> Attributes</h3><div class="g_text"> <table width='100%'> <tr> <td width='15%'>Strength:</td> <td width='35%'><?= $english_format_number = number_format($player->strength);?></td> <td width='15%'>Defense:</td> <td width='35%'><?= $english_format_number = number_format($player->defense);?></td> </tr> <tr> <td width='15%'>Speed:</td> <td width='35%'><?= $english_format_number = number_format($player->speed);?></td> <td width='15%'>Total:</td> <td width='35%'><?= $english_format_number = number_format($player->total);?></td> </tr> </table></div></div> Quote Link to comment https://forums.phpfreaks.com/topic/160551-outputting-instead-of-echoing/#findComment-847325 Share on other sites More sharing options...
KevinM1 Posted June 1, 2009 Share Posted June 1, 2009 You're updating the db values, but you're not updating the player object's values at the same time. Changes in the db aren't magically transferred to the player. So, the changes are staggered as you don't refresh the player object until the entire page is reloaded. Quote Link to comment https://forums.phpfreaks.com/topic/160551-outputting-instead-of-echoing/#findComment-847331 Share on other sites More sharing options...
seany123 Posted June 1, 2009 Author Share Posted June 1, 2009 so what do you suggest that i do? Quote Link to comment https://forums.phpfreaks.com/topic/160551-outputting-instead-of-echoing/#findComment-847337 Share on other sites More sharing options...
KevinM1 Posted June 1, 2009 Share Posted June 1, 2009 so what do you suggest that i do? Well, you need to update the $player before attempting to show that it's changed. What I'd do is simply modify the $player with whatever changes (if any) were made, then display those changes. If any changes were made, then I'd update the db at the end of the script. Quote Link to comment https://forums.phpfreaks.com/topic/160551-outputting-instead-of-echoing/#findComment-847341 Share on other sites More sharing options...
seany123 Posted June 1, 2009 Author Share Posted June 1, 2009 okay so i thought what i could do is have a redirect at the end of every php code... which does the fix the problem but means now its obviously not echoing the output, so ive fixed one problem by createing another >.< Quote Link to comment https://forums.phpfreaks.com/topic/160551-outputting-instead-of-echoing/#findComment-847352 Share on other sites More sharing options...
seany123 Posted June 2, 2009 Author Share Posted June 2, 2009 does anyone know? Quote Link to comment https://forums.phpfreaks.com/topic/160551-outputting-instead-of-echoing/#findComment-847974 Share on other sites More sharing options...
laffin Posted June 2, 2009 Share Posted June 2, 2009 No, he said; Update your variables before Updating the DB. Quote Link to comment https://forums.phpfreaks.com/topic/160551-outputting-instead-of-echoing/#findComment-848036 Share on other sites More sharing options...
seany123 Posted June 4, 2009 Author Share Posted June 4, 2009 yeah but i would prefer it done my way. Quote Link to comment https://forums.phpfreaks.com/topic/160551-outputting-instead-of-echoing/#findComment-849338 Share on other sites More sharing options...
laffin Posted June 4, 2009 Share Posted June 4, 2009 Thats kinda silly isnt it. Obviously yer creating a lot more work than is needed, if you would have taken the advice. Which makes perfect sense But now yer doing all these extravagent tricks, which dont work. So Q? Why ask for advice/help if yer intent on sticking with your way? I dont know about the rest, but that seems kinda rude... Quote Link to comment https://forums.phpfreaks.com/topic/160551-outputting-instead-of-echoing/#findComment-849356 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.