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
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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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++;
}

?>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

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.

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.