Jump to content

[SOLVED] PHP/MySQL question


freelancer

Recommended Posts

Hey freaks, I would like to know how can be this kinda script developed where for example here: http://www.overload-gaming.org/v3/clanwars/index.php

 

I have my own script where you can add items to database tables, and then selecting them at other page, but I would like to create same as in this page above, where you also view/add details to another link, like var's comes as index.php?page=matches&id=2.

 

Can someone give any tutorial or lead me :P Hope you understand what i meant

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/39014-solved-phpmysql-question/
Share on other sites

If you allready know how to execute queries then this kind of thing is easy. Say you have a table (articles).

 

CREATE TABLE articles (
  id INT PRIMARY KEY,
  title VARCHAR(80),
  data TEXT
);

 

Now, on list.php you would create a query that selects all titles and id's. In your while loop, you create links to detail.php. eg;

 

<?php
  while ($row = mysql_fetch_assoc($result)) {
    echo "<a href='detail.php?id={$row['id']}'>{$row['title']}</a><br />";
  }
?>

 

Then.... on detail.php you run a query to retrieve the data associated with the id passed through the url (of course you neeed to sanitise the data first). eg;

 

<?php
  $sql = "SELECT data FROM articles WHERE id = '{$_GET['id']}'";
?>

 

Hope that helps.

Thanks mate I figured it out and it works, but now I got problem like this : when I add something to table, then at my page where is link to details, link is corrent ( id=1 ), now when I add second record, url to this goes also correctly ( id=2 ), but now if I delete first record, then in any url id goes to same. How I can fix this?

This is first page, where you can go to details page

<?php
include("config.php");
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM results ORDER BY id DESC";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

?>

<table border="0" cellspacing="0" cellpadding="2" class="results">
<tr align="left">
<th width="61">Date</th>
<th width="98">Opponent</th>
<th width="67">Map</th>
<th width="65">Score</th>
<th width="104">Type</th>
<th align="center">Details</th>
</tr>

<?php
  while ($row = mysql_fetch_assoc($result)) {
  
$i=0;
while ($i < $num) {
$res=mysql_result( $result,$i,"res");
$date=mysql_result($result,$i,"date");
$opponent=mysql_result($result,$i,"opponent");
$map=mysql_result($result,$i,"map");
$score=mysql_result($result,$i,"score");
$type=mysql_result($result,$i,"type");
// $info=mysql_result($result,$i,"info");
$details=mysql_result($result,$i,"details");

// Color variables


switch($res) {
  case 'Win' : $status = "#008000"; break;
  case 'Lost' : $status = "#FF0000"; break;
  case 'Draw' : $status = "#CC9900"; break;
}

echo "<tr align=\"left\">";
echo "<td>$date</td>";
echo "<td>$opponent</td>";
echo "<td>$map</td>";
echo "<td><font color=\"$status\">$score</font></td>";
echo "<td>$type</font></td>";
echo "<td><center><a target=\"_blank\" href='detail.php?id={$row['id']}'><img border=\"0\" src=\"images/icon/Arrow.gif\"></a><center></td>";
echo "</tr>";
$i++;
}
}
echo "</table>";
?>

 

Here is detail.php:

<?php
include("admin/config.php");
@mysql_select_db($database) or die( "Unable to select database");
$sql=" SELECT details FROM results WHERE id='{$_GET['id']}'";
$result2=mysql_query($sql);
$num=mysql_numrows($result2);
mysql_close();

$i=0;
while ($i < $num) {

$detailshow=mysql_result($result2,$i,"details");

echo $detailshow;
$i++;
}

?>

 

thorpe your code example so good but what he's asking how to add 2 id's to one user as what i understand.

 

if that's true then when a user join's the site then you have to enter a random number to a int field then use that as a second id just an idear.

 

The use of a random number is becouse you can not set the database to increment more then once.

Hey I fixed it, I added $id=mysql_result($result,$i,"id"); and to table url href='detail.php?id=$id' now It works as I want, but how I can include it in my web, not in different window? Or how could this look if I want url with 2 variables like index.php?page=results&id=$id ?

If you want it in the same page removed the target="_blank" from the url. The rest I'm afraid your going to need to come up with yourself. A big part of programming is learning to think. Youve been handed a pretty simple example, just build on that.

 

PS; In your results page. If your only expecting one record (which you are) you should not use a while() loop.

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.