Jump to content

Help needed again please!


kingsbest

Recommended Posts

I have a members.php page with following code-

$result = mysql_query("SELECT * FROM members WHERE club='fent' ORDER BY `members`.`id` ASC");
while ($row = mysql_fetch_array($result)){
$percent = $row['played'] > 0 ? round($row['points']/$row['played']*100, 2) : 0;
   echo "<tr>";
   echo "<td class='h5'><a href='gamehistory.php?lc_URL=".$row['id']."'>Games</a></td>";
  echo "<td class='h5'>" . $row['ref'] . "</td>";
   echo "<td class='h5'>" . $row['name'] . "</td>";
  echo "<td class='h5'>" . $row['grade'] . "</td>";
  echo "<td class='h5'>" . $row['club'] . "</td>";
  echo "<td class='h5'>" . $row['played'] . "</td>";
  echo "<td class='h5'>" . $row['won'] . "</td>";
  echo "<td class='h5'>" . $row['drawn'] . "</td>";
  echo "<td class='h5'>" . $row['lost'] . "</td>";
  echo "<td class='h5'>" . $row['points'] . "</td>";
  echo "<td class='h5'>" . $percent. "</td>";
  echo "</tr>";

 

I would like to make it so that i can click on the Games link and it go to gamehistory.php and using the id from the click make it display all the games this player has played from the database, can this be done? I'm a complete newbie and would appreciate any help, it took me several days of studying and watching videos on php to work out how to create the dynamic link on this page but im now stuck on what to put on gamehistory.php, also please let me know if i need to add anything to the above code to make it more secure.

Best wishes,

John.

 

 

 

Link to comment
Share on other sites

if this is how you're creating the link:

 

gamehistory.php?lc_URL=".$row['id']

 

then on gamehistory.php you can grab the id with this code:

 

$id = $_GET['lc_URL'];

 

now you want to use something like:

 

mysql_query("select * from `games` where `memberID` = '$id'");

 

p.s. I invented the table `games` and the field `memberID` just for the purpose of an example. replace with your actual table and filed names.

Link to comment
Share on other sites

yeah, no problem, you should get the info from the link with $_GET[''], and than use that to look in your database again:

 

$memberid = $_GET['lc_URL'];

$result = mysql_query("SELECT * FROM playedgames WHERE memberid = '$memberid'");

while ($row = mysql_fetch_array($result)){

echo $row['gamename'];
}	

 

EDIT: sorry, same solution as the one above me that dit post his answer a littlebit faster :P

Link to comment
Share on other sites

(just as a reference)

 

all variables passed through an url string (like you have) can be grabbed with $_GET['var_name'];

 

so in this example: pagename.php?var1=Hello&var2=World

 

echo $_GET['var1'] . ' ' . $_GET['var2'];

 

would print "Hello World".

 

Alternatively, all variables passed through an html form (using method="post") can be grabbed on the next page with $_POST['variable_or_field_name'];

 

Hope this helps

Link to comment
Share on other sites

Wow thanks for all the replies, i will give your solution a try :)

PHP blows my mind and there often so many ways of solving a problem, knowing which one is best is tough to find out if your novice.

Once again many thanks for your speedy response, hope it works.

Best wises,

John.

Link to comment
Share on other sites

  • 3 weeks later...

I would now like to try sending the name of a player to the next page

and have all the chess games that this player has played show on the page, my code is-

 

<?php include("includes/db_connect.php");
mysql_select_db($dbname, $con) or die ($dbname);
echo "
<tr>
<td class='h5'>id</td>
<td class='h5'>Ref</td>
<td class='h5'>Name</td>
<td class='h5'>grade</td>
<td class='h5'>club</td>
<td class='h5'>played</td>
<td class='h5'>Won</td>
<td class='h5'>Drawn</td>
<td class='h5'>Lost</td>
<td class='h5'>Points</td>
<td class='h5'>&#37;</td>
</tr>";

$result = mysql_query("SELECT * FROM members WHERE club='fenton' ORDER BY `members`.`id` ASC");
while ($row = mysql_fetch_array($result)){
$percent = $row['played'] > 0 ? round($row['points']/$row['played']*100, 2) : 0;
echo "<tr>";         
echo "<td class='h5'>" . $row['id'] . "</td>";
echo "<td class='h5'>" . $row['ref'] . "</td>";
echo "<td class='h5'><a href='gamehistory.php?lc_URL={$row['name']}'>" . $row['name'] . "</a></td>";
echo "<td class='h5'>" . $row['grade'] . "</td>";
echo "<td class='h5'>" . $row['club'] . "</td>";
echo "<td class='h5'>" . $row['played'] . "</td>";
echo "<td class='h5'>" . $row['won'] . "</td>";
echo "<td class='h5'>" . $row['drawn'] . "</td>";
echo "<td class='h5'>" . $row['lost'] . "</td>";
echo "<td class='h5'>" . $row['points'] . "</td>";
echo "<td class='h5'>" . $percent. "</td>";
echo "</tr>";

}

echo "</table>";
mysql_close($con);
?>

gives the following URL-http://localhost/modsite_new/gamehistory.php?lc_URL=Bloggs Joe

The code on the next page-


<?php 
require("includes/db_connect.php");  
mysql_select_db($dbname, $con) or die ($dbname);

echo"
<form action='gamehistory.php' method='get'>
<p><input type='hidden' name='lc_URL' /></p>
</form>";
$id = $_GET['lc_URL'];
$result = mysql_query("select * FROM `scorecards` WHERE player = '$id'");
while ($row = mysql_fetch_array($result)){
echo "<tr>";      
echo "<td class='h5'>" . $row['id'] . "</td>";
echo "<td class='h5'>" . $row['match_code'] . "</td>";
echo "<td class='h5'>" . $row['board_number'] . "</td>";
echo "<td class='h5'>" . $row['player'] . "</td>";
echo "<td class='h5'>" . $row['home_grade'] . "</td>";
echo "<td class='h5'>" . $row['game_result'] . "</td>";
echo "<td class='h5'>" . $row['aplayer'] . "</td>";
echo "<td class='h5'>" . $row['away_grade'] . "</td>";
echo "</tr>";

}
echo "</table>";
mysql_close($con);
?>

 

Can you see any reason why this doesn't show anything?

 

Best wishes and thanks for all the help so far!

John.

 

Link to comment
Share on other sites

Keep in mind that variables passed with GET method have a length limit and are always seen in the url. if you use POST they will not be seen and there's no limit.

 

Back to your code:

on page two, you have a hidden element that i not doing anything:

<input type='hidden' name='lc_URL' />

Just thought I would mention that. I don't know if you'll need it later or not, but it's currently an element with just a name but no value, you're gonna need something like:

<input type='hidden' name='lc_URL'  value='SOME VALUE HERE'/>

You also have this inside a form with no submit button. That is not going to do anything.

 

Another thing to consider: Keep things tidy so you don't confuse yourself (and others). You're using this:

<a href='gamehistory.php?lc_URL={$row['name']}'>

to pass the value of 'name' across to the next page, but you call it 'lc_URL' when passing it, and 'id' when grabbing it on the next page. Just call it 'name' everywhere. Do the same in your databases... when i came out of the first one it was called name, why is it called player in the second one? Keep things simple. You now have name, lc_URL, id and player and they're all the same value. Not a good idea.

 

you're also mixing styles a bit when you write this: (not that it's a problem, it's just not coherent)

echo "<td class='h5'><a href='gamehistory.php?lc_URL={$row['name']}'>" . $row['name'] . "</a></td>";

why not use the same way of displaying the variable value?

either this (my preference):

echo "<td class='h5'><a href='gamehistory.php?lc_URL=" . $row['name'] . "'>" . $row['name'] . "</a></td>";

or this:

echo "<td class='h5'><a href='gamehistory.php?lc_URL={$row['name']}'>{$row['name']}</a></td>";

 

 

I'm kind of confused by your <form> in the second page... things work the other way around:

you place the <form> on the first page, with all the variables you need to pass to the second page and use a submit button inside it... here's an example:

 

page1.php:

echo '<form action="page2.php" method="post">
<input type="hidden" name="var1" value="valueOfVariable1"/>
<input type="hidden" name="var2" value="valueOfVariable2"/>
<input type="hidden" name="var3" value="valueOfVariable3"/>
<input type="submit" name="submitbutton" value="Send to next page"/>
</form>';

then on page2.php you could grab them all with:

echo 'Variable 1 has value: '.$_POST['var1'];
echo 'Variable 2 has value: '.$_POST['var2'];
echo 'Variable 3 has value: '.$_POST['var3'];

 

with this in mind, you url is correct and the value should be getting to the second page... try echo $_GET['lc_URL']; just to be sure it's getting there. If it is, then there is no entry in your database table `scorecards` where the field `player` has that value.

 

I sort of got carried away here, sorry about that. Hope it helps though.

Link to comment
Share on other sites

Hi WebStyles and thanks for your help, i think i have cracked it with your help, here is my code please let me know what you think-

$result = mysql_query("SELECT * FROM members WHERE club='fen' ORDER BY `members`.`id` ASC");
while ($row = mysql_fetch_array($result)){
$percent = $row['played'] > 0 ? round($row['points']/$row['played']*100, 2) : 0;
echo "<tr>";         
echo "<td class='h5'>" . $row['id'] . "</td>";
echo "<td class='h5'>" . $row['ref'] . "</td>";
echo "<td class='h5'><a href='gamehistory.php?name_URL=".$row['name']."'>" . $row['name'] . "</a></td>";
echo "<td class='h5'>" . $row['grade'] . "</td>";
echo "<td class='h5'>" . $row['club'] . "</td>";
echo "<td class='h5'>" . $row['played'] . "</td>";
echo "<td class='h5'>" . $row['won'] . "</td>";
echo "<td class='h5'>" . $row['drawn'] . "</td>";
echo "<td class='h5'>" . $row['lost'] . "</td>";
echo "<td class='h5'>" . $row['points'] . "</td>";
echo "<td class='h5'>" . $percent. "</td>";

echo "</tr>";

and the page the link goes to has this code-

 

$name = $_GET['name_URL'];

$result = mysql_query("SELECT * FROM scorecards WHERE name = '$name' OR aname = '$name'");

while ($row = mysql_fetch_array($result)){


echo "<tr>";      
echo "<td class='h5'>" . $row['id'] . "</td>";
echo "<td class='h5'>" . $row['match_code'] . "</td>";
echo "<td class='h5'>" . $row['board_number'] . "</td>";
echo "<td class='h5'><a href='gamehistory.php?name_URL=".$row['name']."'>" . $row['name'] . "</a></td>";
echo "<td class='h5'>" . $row['home_grade'] . "</td>";
echo "<td class='h5'>" . $row['game_result'] . "</td>";
echo "<td class='h5'><a href='gamehistory.php?name_URL=".$row['aname']."'>" . $row['aname'] . "</a></td>";
echo "<td class='h5'>" . $row['away_grade'] . "</td>";
echo "</tr>";

}
echo "</table>";

"name" is the home player and "aname" is the away player, the code is now taking out any games from the URL variable, at first i could not make it display games if the player was in the away field, is this a good way of doing this? Keep in mind that it needs to be simple for me to understand how it works. Also when i click on the links on the second page it displays the games played by the opponent which is what i wanted.
Best wishes and thanks,
John.

Link to comment
Share on other sites

yeah, if it's working, seems fine for what you need. just a little note:

where you have this:

$result = mysql_query("SELECT * FROM members WHERE club='fen' ORDER BY `members`.`id` ASC");

you don't realli need to specify the table in the ORDER BY part, since you're only selecting from one table, so you could just write this:

$result = mysql_query("SELECT * FROM `members` WHERE club='fen' ORDER BY `id` ASC");

also ASC is the default value, so you don't need it either:

$result = mysql_query("SELECT * FROM `members` WHERE club='fen' ORDER BY `id`");

and since you used back ticks for the id and members fields, keep it coherent and also use them in club:

$result = mysql_query("SELECT * FROM `members` WHERE `club` = 'fen' ORDER BY `id`");

(even though some people may not agree with their use, back ticks may be helpful in the future, if you accidentally use a mysql reserved word for a field name and cannot figure out why it's not working)

 

Now, if you want to improve on it a bit, check out mysql_real_escape_string manual, since you're sending a field value over an url, you may need to protect yourself against SQL Injection attacks.

 

 

Glad to see you got it working!

Link to comment
Share on other sites

Thanks again for the advice, i was planning on updating the database through php myadmin and not a CMS, is this more secure? Plus had a look at mysql_real_escape_string and added the following line=$name = mysql_real_escape_string($name); t0-

$name = $_GET['name_URL'];

$result = mysql_query("SELECT * FROM scorecards WHERE name = '$name' OR 'aname' = '$name'");

$name = mysql_real_escape_string($name);

while ($row = mysql_fetch_array($result)){


echo "<tr>";      
echo "<td class='h5'>" . $row['id'] . "</td>";
echo "<td class='h5'>" . $row['match_code'] . "</td>";
echo "<td class='h5'>" . $row['board_number'] . "</td>";
echo "<td class='h5'><a href='gamehistory.php?name_URL=".$row['name']."'>" . $row['name'] . "</a></td>";
echo "<td class='h5'>" . $row['home_grade'] . "</td>";
echo "<td class='h5'>" . $row['game_result'] . "</td>";
echo "<td class='h5'><a href='gamehistory.php?name_URL=".$row['aname']."'>" . $row['aname'] . "</a></td>";
echo "<td class='h5'>" . $row['away_grade'] . "</td>";
echo "</tr>";

Is this what you meant? Apologies for my lack of knowledge, only started a few months ago with a couple of books on php and loads of hours watching you tube tutorials.

Thanks again.

John.

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.