Jump to content

[SOLVED] Repetitive complexity - problem with copied pasted function and slightly modded


Recommended Posts

Morning Everyone, (12:50am *yawn*)

 

I'm going to try and explain this as simple and with as little code as possible(the code gets repetitive, it just sets variables)

 

Now, when a user logs into my website, they are automatically redirected to their "Profile" page, Which funny enough displays their profile. Now I have a page called "attack.php" which lists every user on the website, and it creates a link for each user. This link then sends them to(this is the hard to explain part) player_interaction.php

 

in this format:

of course within the * is a integer.

 

player_interaction.php displays the profile of the person you clicked the link for on the previous page, attack.php

 

and the attack.php page which displays all the users has a URL like this:

 

Now, I have a function I use to grab data from the database for whoever the person is logged in as, now I copied and pasted that function and modified it to select the same data, but for the person's profile you are looking at. But nothing is being displayed.

 

So, I know this is alot to take in, but I'm almost finished  ;), here is the function I use on the users Main page to display their profile:

<?php
// Select fields from the user table
function player_table($select){
    $where = $_SESSION['playerid'];
    $rs = mysql_connect("localhost", "ace_ACE", "*****");
    mysql_select_db("ace_cf", $rs);
    $sql = "SELECT `" . $select . "` FROM `cf_users` WHERE `id`='" . $where . "' LIMIT 1";
    $rs = mysql_query($sql) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error());
    while($row = mysql_fetch_assoc($rs)){
        if(isset($row[$select])){return $row[$select];}
    }
}

 

and here is the modifed version of it that I use for viewing other users profiles:

<?php
// Select fields from other users table
function player_table_other($select,$other_player_id){
    $rs = mysql_connect("localhost", "ace_ACE", "*****");
    mysql_select_db("ace_cf", $rs);
    $sql = "SELECT `" . $select . "` FROM `cf_users` WHERE `id`='" . $other_player_id . "' LIMIT 1";
    $rs = mysql_query($sql) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error());
    while($row = mysql_fetch_assoc($rs)){
        if(isset($row[$select])){return $row[$select];}
    }
}

 

Now, keeping in mind that the first function works, and the second doesn't, here is "attack.php" which displays all the users and has a <hidden field for holding each users ID which is then used on the next page to grab the correct profile.

 

so here is attack.php :

<?php // players base

/* includes */
include_once '/home/ace/public_html/conflictingforces/functions.php';
include_once '/home/ace/public_html/conflictingforces/weapons.php';
include_once '/home/ace/public_html/conflictingforces/armors.php';
include_once '/home/ace/public_html/conflictingforces/vehicles.php';

player_session();

/**********************/
// Player Select Variables //
/**********************/

// User Information
$player_accountid = player_table("id");
$player_username = player_table("username");
$player_email = player_table("email");
$player_race = player_race();

// Military Effectiveness
$player_strikeaction = player_table("strikeaction");
$player_defenceaction = player_table("defenceaction");
$player_covertaction = player_table("covertaction");

// General Statistics
$player_level = player_table("level");
$player_currentexp = player_table("currentexp");
$player_neededexp = player_table("neededexp");
$player_skilllevel = player_table("skilllevel");
$player_skillpoints = player_table("skillpoints");
$player_strength = player_table("strength");
$player_agility = player_table("agility");
$player_intelligence = player_table("intelligence");

// Talent Statistics
// the same, just more repetitive variables

// Alliance Information
// the same, just more repetitive variables

// Inventory
// the same, just more repetitive variables


// Equipped Items

// Names
$equippedweaponname = select_array($weapons,$player_weaponid,"weapon");
$equippedarmorname = select_array($armors,$player_armorid,"armor");
$equippedvehiclename = select_array($vehicles,$player_vehicleid,"vehicle");

// ID
$equippedweaponid = select_array($weapons,$player_weaponid,"id");
$equippedarmorid = select_array($armors,$player_armorid,"id");
$equippedvehicleid = select_array($vehicles,$player_vehicleid,"id");


// display information for each player in a table
$link = mysql_connect( "localhost", "ace_ACE", "*****" );
if ( ! $link ) {
die( "Couldn't connect to MySQL: ".mysql_error() );
}

mysql_select_db( "ace_cf", $link )
or die ( "Couldn't open 'ace_cf': ".mysql_error() );

// workout the page pagination limits etc
    $limit          = 5;
    $vpage 			= (int)$_GET['vpage'];
    $query_count    = "SELECT count(*) as totalcount FROM cf_users";    
    $result_count   = mysql_query($query_count);
$tmp = mysql_fetch_array($result_count);
$totalrows   = $tmp['totalcount'];

    if(empty($vpage)){
        $vpage = 1;
    }

    $limitvalue = $vpage * $limit - ($limit); 
    $query  = "SELECT * FROM cf_users LIMIT $limitvalue, $limit";        
    $result = mysql_query($query) or die("Error: " . mysql_error()); 

    if(mysql_num_rows($result) == 0){
        echo("Nothing to Display!");
    }


//print "<p>$num_rows people have money</p>\n";
echo '<div id = "attack">';
echo '<br>


<table align="center" class = "fix">

<tr>
<td colspan="4">
<center><b>Players</b></center>
</td>
</tr>

<tr>

<td><b>Name</b></td>

<td><b>Rank</b></td>

<td><b>Race</b></td>

<td><b>Money</b></td>

</tr>

<tr>
<td colspan="4">
</td>
</tr>';


while ($a_row = mysql_fetch_assoc( $result ) ) {

	$all_id = stripslashes($a_row['id']);
	$all_name = stripslashes($a_row['username']);
	$all_rank = stripslashes($a_row['rank']);
	$all_race = stripslashes($a_row['race']);
	$all_money = stripslashes($a_row['money']);

// change the players race from a integer into a string
if($all_race == 0) {
$all_race = "British SAS";
}	
elseif($all_race == 1) {
$all_race = "Navy Seal";
}
elseif($all_race == 2) {
$all_race = "Russian Spetsnaz";
}
elseif($all_race == 3) {
$all_race = "Australian SAS";
}

echo	'

<tr>

<td>';

echo "<a href=\"http://www.crikeygames.com.au/conflictingforces/index.php?page=player_interaction&player_interaction=$all_id\">" . $all_name . "</a>";
echo '<form action="http://www.crikeygames.com.au/conflictingforces/index.php?page=player_interaction&player_interaction=$all_id"
method="post">';
echo '<input type="hidden" name="otherplayersid" value='.$all_id.'>';
echo '</form>';

echo '</td>

<td>' . $all_rank . '</td>

<td>' . $all_race . '</td>

<td>$' . $all_money . '</td>

</tr>';
}
echo '</table>';

echo '<center>';

if($vpage != 1){ 
        $pageprev = $vpage--;
        
        echo("<a href=\"http://www.crikeygames.com.au/conflictingforces/index.php?page=attack&vpage=$pageprev\">PREV</a> "); 
    }else{
        echo("PREV ");
    }

$numofpages = $totalrows / $limit; 
    
    for($i = 1; $i <= $numofpages; $i++){
        if($i == $vpage){
            echo($i." ");
        }else{
            echo("<a href=\"http://www.crikeygames.com.au/conflictingforces/index.php?page=attack&vpage=$i\">$i</a> ");
        }
    }


    if(($totalrows % $limit) != 0){
        if($i == $vpage){
            echo($i." ");
        }else{
            echo("<a href=\"http://www.crikeygames.com.au/conflictingforces/index.php?page=attack&vpage=$i\">$i</a> ");
        }
    }

    if(($totalrows - ($limit * $vpage)) > 0){
        $pagenext = $vpage++;
         
        echo("<a href=\"http://www.crikeygames.com.au/conflictingforces/index.php?page=attack&vpage=$pagenext\">NEXT</a> "); 
    }else{
        echo("NEXT "); 
    }
    
    mysql_free_result($result);

echo '</center>';

echo '</div>';

?>

 

Note that with the pagination, The links are mixed up, like if I'm on page 2, the number 1 will be plain text, but the number 2 will be a link to page 1, etc, if someone could help me fix that minor problem it would be most appreciated.

 

now attack.php then sends the <hidden field containing the users ID of whoever you clicked on, and player_interaction.php picks that up.

 

here is player_interaction.php :

<?php
/*****************************/
// Other Player Select Variables  //
/*****************************/
$other_players_id = $_POST["otherplayersid"]; // the ID sent by the <hidden field

// User Information -  your information
$other_player_accountid = player_table_other("id",$other_players_id);
$other_player_username = player_table_other("username",$other_players_id);
$other_player_race = player_race_other($other_players_id);

// Military Effectiveness
// more of the repetitive variables

// General Statistics
// more of the repetitive variables

// Talent Statistics - yours
// more of the repetitive variables

// Alliance Information - yours
// more of the repetitive variables

// Inventory - yours
// more of the repetitive variables

// Equipped Items

// Names - yours
$other_equippedweaponname = select_array($weapons,$other_player_weaponid,"weapon");
$other_equippedarmorname = select_array($armors,$other_player_armorid,"armor");
$other_equippedvehiclename = select_array($vehicles,$other_player_vehicleid,"vehicle");

// ID - yours
$other_equippedweaponid = select_array($weapons,$other_player_weaponid,"id");
$other_equippedarmorid = select_array($armors,$other_player_armorid,"id");
$other_equippedvehicleid = select_array($vehicles,$other_player_vehicleid,"id");


/***************************************************************************/
// workout the end of the pages URL
$player_interaction = (int)$_GET['player_interaction'];
if(empty($player_interaction)){
        $player_interaction = $other_players_id;
    }

?>

<div id = "base">
<br>

<!-- Left Column -->
<div id = "leftcolumn">

<table align="center" class = "fix">


<tr>
<td colspan="2">
<center><b>User Information</b></center>
</td>
</tr>

<tr>
<td><b>ID</b></td>
<td><?php echo $other_player_accountid; ?></td>
</tr>

<tr>
<td><b>Name</b></td>
<td><?php echo $other_player_username; ?></td>
</tr>

<tr>
<td><b>Race</b></td>
<td><?php echo $other_player_race; ?></td>
</tr>


</table>

<br>

<table align="center" class = "fix">


<tr>
<td colspan="2">
<center><b>General Statistics</b></center>
</td>
</tr>

<tr>
<td><b>Money</b></td>
<td><?php echo $other_player_money; ?></td>
</tr>

<tr>
<td><b>Level</b></td>
<td><?php echo $other_player_level; ?></td>
</tr>

<tr>
<td><b>Strength</b></td>
<td><?php echo $other_player_strength; ?></td>
</tr>

<tr>
<td><b>Agility</b></td>
<td><?php echo $other_player_agility; ?></td>
</tr>

<tr>
<td><b>Intelligence</b></td>
<td><?php echo $other_player_intelligence; ?></td>
</tr>


</table>

<br>

<table align="center" class = "fix">


<tr>
<td colspan="2">
<center><b>Alliance Information</b></center>
</td>
</tr>

<tr>
<td><b>Alliance ID</b></td>
<td><?php echo $other_player_allianceid; ?></td>
</tr>

<tr>
<td><b>Alliance Name</b></td>
<td><?php echo $other_player_alliancename; ?></td>
</tr>

<tr>
<td><b>Position</b></td>
<td><?php echo $other_player_allianceposition; ?></td>
</tr>


</table>

</div>


<!-- Right Column -->
<div id = "rightcolumn">

<table align="center" class = "fix">


<tr>
<td colspan="2">
<center><b>Military Effectiveness</b></center>
</td>
</tr>

<tr>
<td><b>Strike Action</b></td>
<td><?php echo number_format($other_player_strikeaction); ?></td>
</tr>

<tr>
<td><b>Defence Action</b></td>
<td><?php echo number_format($other_player_defenceaction); ?></td>
</tr>

<tr>
<td><b>Covert Action</b></td>
<td><?php echo number_format($other_player_covertaction); ?></td>
</tr>


</table>

<br>

<table align="center" class = "fix">


<tr>
<td colspan="2">
<center><b>Talent Statistics</b></center>
</td>
</tr>

<tr>
<td><b>Talent ID</b></td>
<td><?php echo $other_player_talentid; ?></td>
</tr>

<tr>
<td><b>Talent Name</b></td>
<td><?php echo $other_player_talentname; ?></td>
</tr>

<tr>
<td><b>Talent Level</b></td>
<td><?php echo $other_player_talentlevel; ?></td>
</tr>

</table>


</div>


</div>

 

and that isn't displaying any of the echo's in the table.

Also I am using another function I made called select_array()

and that is here:

<?php
// Select 1 field from an Array
function select_array($array,$id,$field) {

     return $array[$id][$field]; 

}

 

ok, to summarise everything I just said. The problem is that in player_interaction.php nothing is being displayed in the table, and on attack.php the pagination links are messing up.

 

Thanks,

 

Regards ACE

in the function "player_table" you have this line:

 

$where = $_SESSION['playerid'];

 

Have you started the session before getting the value?

 

session_start();

 

EDIT:

 

Sorry, just ignore me.. should have read the post better  :P

in the file player_interaction.php, have you checked if the variable $other_players_id is empty, or if player_table_other() returns anything? what is the response if you do this:

 

$other_players_id = $_POST["otherplayersid"]; 
echo "Other players Id: $other_players_id \n"; // echo the value from POST

$other_player_accountid = player_table_other("id",$other_players_id);
echo "Other players Account id: $other_player_accountid \n"; // echo the value from player_table_other

 

hmm, seems to be undefined  :-\

Notice: Undefined index: otherplayersid in /home/ace/public_html/conflictingforces/lib/player_interaction.php on line 132

Other players Id: Other players Account id:

ok, so I missed out a submit button lol. But I don't particularly want to use a submit button, I just want to use a hyperlink to get to each user's profile.

 

My friend said something about using javascript, something like this:

<a href='javascript:document.getElementbyId("form_id").submit()'>Link Text</a>

 

but I don't understand what he means, does anyone know how I can just make the link go to the user's page, and it will submit the <hidden field through the <a href tag?

 

 

I'm still getting the same error  :-\ but here is the code I have now. For trying to submit the id through javascript.

<?php
echo '<td>';

?>
<script>
function js_link(){
location.href= "http://www.crikeygames.com.au/conflictingforces/index.php?page=player_interaction&player_interaction='<?=$all_id ?>"
document.getElementbyId("otherplayersid").submit();
}
</script>

<form id="otherplayersid">
<input type="hidden" name="otherplayersid" value="<?=$all_id ?>">
</form>

<?php

echo "<a href=\"javascript:js_link()\">" . $all_name . "</a>";

echo '</td>

 

and that seems to be putting the ID's of each person into the link alright, here's the Source from Mozilla Firefox for my account in the user list:

<td><script>
function js_link(){
location.href= "http://www.crikeygames.com.au/conflictingforces/index.php?page=player_interaction&player_interaction='1"
document.getElementbyId("otherplayersid").submit();
}
</script>

<form name="otherplayersid" method="post">
<input type="hidden" name="otherplayersid" value="1">
</form>

<a href="javascript:js_link()">ACE</a></td>

 

here is my latest script:

<?php
echo '<td>';

?>

<form id="otherplayersid" method="post">
<input type="hidden" name="otherplayersid" value="<?=$all_id ?>">
</form>
<script>
function js_link(){
location.href= "http://www.crikeygames.com.au/conflictingforces/index.php?page=player_interaction"
document.getElementbyId("otherplayersid").submit();
}
</script>



<?php

echo "<a href=\"javascript:js_link()\">" . $all_name . "</a>";

echo '</td>';
?>

anybody got any ideas at all? I'm on the brink of self-destruction  :o

 

I will state again what my 2 problems are, as this is quite a long thread.

 

Problem 1)

 

I am having trouble submitting the users ID across to the next page using a hidden input and javascript.

 

Problem 2)

 

The pagination links are alittle mixed up, for example, when I am on "Page 1" the "NEXT" link, is linking to "Page 1" instead of "Page 2"

If you want to use javascript to submit the form, give it a name, and submit using that name:

 

<?php
echo '<td>';

?>

<form id="otherplayersid" method="post" name="otherplayersid" action="http://www.crikeygames.com.au/conflictingforces/index.php?page=player_interaction">
<input type="hidden" name="otherplayersid" value="<?=$all_id ?>">
</form>
<?php

echo "<a href=\"javascript:document.otherplayersid.submit();\">" . $all_name . "</a>";
echo '</td>';
?>

Can you post some of the generated source? including the form and link..

 

EDIT:

 

Never mind.. The name of the form, and the name of the hidden field is the same.. try to change the form name and the javascript..

yeah, I figured that was the case, thats why I've started posting just this section:

<?php
echo '<td>';

?>



<form id="otherplayersid" method="post" name="otherplayersid" action="http://www.crikeygames.com.au/conflictingforces/index.php?page=player_interaction">
<input type="hidden" name="otherplayersid" value="<?=$all_id ?>">
</form>



<?php

echo "<a href=\"javascript:document.otherplayersid.submit();\">" . $all_name . "</a>";

echo '</td>';
?>

 

hmm ok, I am using a while statement to display each user and this link, so, do I use a foreach statement, just for the form and the link part? I dunno.

 

Here's abit more of my code, going as far as the while statement to make things clearer.

<?php
while ($a_row = mysql_fetch_assoc( $result ) ) {

	$all_id = stripslashes($a_row['id']);
	$all_name = stripslashes($a_row['username']);
	$all_rank = stripslashes($a_row['rank']);
	$all_race = stripslashes($a_row['race']);
	$all_money = stripslashes($a_row['money']);

// change the players race from a integer into a string
if($all_race == 0) {
$all_race = "British SAS";
}	
elseif($all_race == 1) {
$all_race = "Navy Seal";
}
elseif($all_race == 2) {
$all_race = "Russian Spetsnaz";
}
elseif($all_race == 3) {
$all_race = "Australian SAS";
}

echo	'

<tr>

<td>';

?>



<form id="otherplayersid" method="post" name="otherplayersid" action="http://www.crikeygames.com.au/conflictingforces/index.php?page=player_interaction">
<input type="hidden" name="otherplayersid" value="<?=$all_id ?>">
</form>



<?php

echo "<a href=\"javascript:document.otherplayersid.submit();\">" . $all_name . "</a>";

echo '</td>

<td>' . $all_rank . '</td>

<td>' . $all_race . '</td>

<td>$' . $all_money . '</td>

</tr>';
}
echo '</table>';

?>

Ok, you need to give each form a unique id and name.. try this then:

 

<?php
while ($a_row = mysql_fetch_assoc( $result ) ) {

	$all_id = stripslashes($a_row['id']);
	$all_name = stripslashes($a_row['username']);
	$all_rank = stripslashes($a_row['rank']);
	$all_race = stripslashes($a_row['race']);
	$all_money = stripslashes($a_row['money']);

// change the players race from a integer into a string
if($all_race == 0) {
$all_race = "British SAS";
}	
elseif($all_race == 1) {
$all_race = "Navy Seal";
}
elseif($all_race == 2) {
$all_race = "Russian Spetsnaz";
}
elseif($all_race == 3) {
$all_race = "Australian SAS";
}

echo	'

<tr>

<td>';

?>



<form id="otherplayersid_<?php echo $all_id;?>" method="post" name="otherplayersid_<?php echo $all_id;?>" action="http://www.crikeygames.com.au/conflictingforces/index.php?page=player_interaction">
<input type="hidden" name="otherplayersid" value="<?=$all_id ?>">
</form>



<?php

echo "<a href=\"javascript:document.otherplayersid_<?php echo $all_id;?>.submit();\">" . $all_name . "</a>";

echo '</td>

<td>' . $all_rank . '</td>

<td>' . $all_race . '</td>

<td>$' . $all_money . '</td>

</tr>';
}
echo '</table>';

?>

I don't know whats wrong with this, it's still not going to the next page when i click the links, just does nothing.

 

Everything seems to be correct according to the pages source(viewing Source in Firefox):

their is my account first in the list:

<form id="otherplayersid_1" method="post" name="otherplayersid" action="http://www.crikeygames.com.au/conflictingforces/index.php?page=player_interaction">

<input type="hidden" name="otherplayersid" value="1">

</form>

 

 

 

<a href="javascript:document.otherplayersid_1.submit();">ACE</a></td>

 

then the next person:

<form id="otherplayersid_2" method="post" name="otherplayersid" action="http://www.crikeygames.com.au/conflictingforces/index.php?page=player_interaction">

<input type="hidden" name="otherplayersid" value="2">

</form>

 

 

<a href="javascript:document.otherplayersid_2.submit();">mastertkd</a></td>

 

and they all got the unique id's.

 

 

The name part on the form is still not unique:

 

<form id="otherplayersid_1" method="post" name="otherplayersid"

 

 

Another solution could be to skip the form part, an use GET instead of post. then all you need is the link:

 

echo "<a href=\"http://www.crikeygames.com.au/conflictingforces/index.php?page=player_interaction&otherplayerid=<?php echo urlencode($all_id);?>\">" . $all_name . "</a>";

with a GET, can I do everything the same as if I was using a POST? such as using a $_POST["whatever"]; and put it into a variable which then can be used to Query the database etc?

 

EDIT: Its working!!! I just needed to make the name="" unique in the form as well, and it is working perfectly now :D

Thanks so much haaglin.

 

now, thats 1 problem down, 1 to go. I believe this one is more simple, just abit of rearranging, But I don't know how to rearrange it lol. It's my page pagination code, it is alittle muddled up.

 

Basically what the bugs in the moment with it are, no matter what page you are on, the number "1" is never a link, which numbers "2", "3" etc are, they are all links to those respective page numbers. And the other bug is, the "NEXT" button actually goes to the previous page, and the "PREV" button actually goes to the next page.

 

here it is:

<?php
if($vpage != 1){ 
        $pageprev = $vpage--;
        
        echo("<a href=\"http://www.crikeygames.com.au/conflictingforces/index.php?page=attack&vpage=$pageprev\">PREV</a> "); 
    }else{
        echo("PREV ");
    }

$numofpages = $totalrows / $limit; 
    
    for($i = 1; $i <= $numofpages; $i++){
        if($i == $vpage){
            echo($i." ");
        }else{
            echo("<a href=\"http://www.crikeygames.com.au/conflictingforces/index.php?page=attack&vpage=$i\">$i</a> ");
        }
    }


    if(($totalrows % $limit) != 0){
        if($i == $vpage){
            echo($i." ");
        }else{
            echo("<a href=\"http://www.crikeygames.com.au/conflictingforces/index.php?page=attack&vpage=$i\">$i</a> ");
        }
    }

    if(($totalrows - ($limit * $vpage)) > 0){
        $pagenext = $vpage++;
         
        echo("<a href=\"http://www.crikeygames.com.au/conflictingforces/index.php?page=attack&vpage=$pagenext\">NEXT</a> "); 
    }else{
        echo("NEXT "); 
    }
?>

yes, just replace $_POST['whatever'] with $_GET['whatever']. But dont forget to secure the values before using it in a query. a simple solution for this is

 

$post = htmlentities(strip_tags($_POST['whatever']));
$get = htmlentities(strip_tags($_GET['whatever']));

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.