PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
jamrop
Forum Commoner
Posts: 80 Joined: Fri May 16, 2003 5:38 pm
Post
by jamrop » Fri May 16, 2003 5:38 pm
Hey I am trying to add a button to each query result that is displayed. I am trying to have a button that will show another page with a decription etc from the chosen anime title
I am getting php to display all my anime titles from the database
I am using mysql as my database.
Here is the code
Code: Select all
$db =mysql_connect("localhost","jamrop", "password");
mysql_select_db("jamrop_uk_db",$db);
$sql = "SELECT * FROM anime";
$rs=mysql_query($sql,$db);
$numofrows =mysql_num_rows($rs);
print "<table border="1"><tr><th>Anime Id </th>";
print "<th>Anime Name</th>";
for ($x = 0; $x <$numofrows; $x++)
{
$row = mysql_fetch_array($rs);
if($x % 2)
{
echo "<tr bgcolor="CCFFCC">\n";
}
else
{
echo "<tr bgcolor ="white">\n";
}
echo "<td>".$row[Anime_id]."</td>";
echo "<td>".$row[Anime_name]."</td>";
}
echo "</table>\n";
?>
Can anyone help
many thanks
patrikG
DevNet Master
Posts: 4235 Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK
Post
by patrikG » Fri May 16, 2003 7:32 pm
Use
LIMIT in your mySQL-query.
It would then read
Code: Select all
//if you want to post data via get, you could also use post, but it would be slightly more complicated than get
extract($_GET); //if "http://www.mysite.net?readfrom=20" $readFrom is assigned 20;
if (!$readFrom) //if $readFrom is not set, it's 0
{$readfrom=0;}
$query="SELECT * FROM anime LIMIT $readFrom,10"
//10 posts from $readFrom will be read
For the buttons at the bottom of your page: use links "
http://www.mysite.net?readfrom=10 " for the first, "
http://www.mysite.net?readfrom=20 " for the second and so on.
You may want to look into mySQL-command "ORDER BY" as it is useful to have your records ordered in some way rather than rely your table's index.
jamrop
Forum Commoner
Posts: 80 Joined: Fri May 16, 2003 5:38 pm
Post
by jamrop » Sat May 17, 2003 4:36 am
thanks for ur help but i think i explained it wrong
say in my query result i have animr title
anime id anime name anime eps
1 naruto 1-26
2 akira 1
what i want to try and achieve it that after anime eps i have a button that gives description of the anime name e.g.
anime id anime name descirption
1 naruto <link button>
2 akira <link button>
there is going to be quite a lot of anime titles, so i want it so the link goes to the same page and knows what anime id it is to get description details.
hard to explain
many thanks
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Sat May 17, 2003 8:30 am
anime id identifies the record/entry (uniquely), right?
So you can use this id to query a specific record, all you have to do is to propagate the id through the link.
e.g. as <a href...>
Code: Select all
<?php
...
echo '<td>', $row['Anime_name'], '</td>';
echo '<td><a href="detail.php?aid=', $row['Anime_id'], '">details</a></td>';
...
?>In the output there will be something like
Code: Select all
<a href="detail.php?aid=5">details</a>fetch the parameter in detail.php and use it as where-clause in your query
Code: Select all
if (isset($_GET['aid']))
{
$query = 'SELECT * FROM anime WHERE Anime_id=' . (int)$_GET['aid'];
...
jamrop
Forum Commoner
Posts: 80 Joined: Fri May 16, 2003 5:38 pm
Post
by jamrop » Sat May 17, 2003 5:36 pm
Hey i am integrating the code u gave me into my php page, but i get no results from my database
have i done something wrong?
code below
Code: Select all
$db =mysql_connect("localhost","jam", "");
mysql_select_db("jam_uk_db",$db);
if (isset($_GET['aid']))
{
$query = 'SELECT * FROM anime WHERE Anime_id=' . (int)$_GET['aid'];
$rs=mysql_query($sql,$db);
$numofrows =mysql_num_rows($rs);
print "<table border="1"><tr><th>Anime Id </th>";
print "<th>Anime Name</th>";
for ($x = 0; $x <$numofrows; $x++)
{
$row = mysql_fetch_array($rs);
if($x % 2)
{
echo "<tr bgcolor="CCFFCC">\n";
}
else
{
echo "<tr bgcolor ="white">\n";
}
echo "<td>".$row[Anime_id]."</td>";
echo '<td><a href="detail.php?aid=', $row['Anime_id'], '">details</a></td>';
echo "<td>".$row[Anime_name]."</td>";
}
echo "</table>\n";
?>
Last edited by
jamrop on Sat May 17, 2003 6:55 pm, edited 1 time in total.
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Sat May 17, 2003 5:55 pm
hm, which version of php do you use?
will tell you.
Does the link contain the
aid=xyz part?
jamrop
Forum Commoner
Posts: 80 Joined: Fri May 16, 2003 5:38 pm
Post
by jamrop » Sat May 17, 2003 5:58 pm
PHP4u Version 2.2
Based on PHP-4.1.0
jamrop
Forum Commoner
Posts: 80 Joined: Fri May 16, 2003 5:38 pm
Post
by jamrop » Sat May 17, 2003 6:01 pm
there is not a link there
for each anime id
jamrop
Forum Commoner
Posts: 80 Joined: Fri May 16, 2003 5:38 pm
Post
by jamrop » Sat May 17, 2003 6:06 pm
jamrop
Forum Commoner
Posts: 80 Joined: Fri May 16, 2003 5:38 pm
Post
by jamrop » Sat May 17, 2003 6:50 pm
hey
this seems to work
Code: Select all
$db =mysql_connect("localhost","jam", "");
mysql_select_db("jam_uk_db",$db);
$sql = 'SELECT * FROM anime' ;
$rs=mysql_query($sql,$db);
$numofrows =mysql_num_rows($rs);
print "<table border="1"><tr><th>Anime Name </th>";
print "<th></th>";
for ($x = 0; $x <$numofrows; $x++)
{
$row = mysql_fetch_array($rs);
if($x % 2)
{
echo "<tr bgcolor="CCFFCC">\n";
}
else
{
echo "<tr bgcolor ="white">\n";
}
echo '<td>', $row['Anime_name'], '</td>';
echo '<td><a href="detail.php?Anime_id=', $row['Anime_id'], '">details</a></td>';
}
echo "</table>\n";
?>
I am not sure if this is the wrong procedure or bad coding.
What is for example i wanted to have the
anime_name result as a link it self to e.g. details.php, is that possible
many thanks
jamie
evilcoder
Forum Contributor
Posts: 345 Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia
Post
by evilcoder » Sat May 17, 2003 7:37 pm
just in the future, please use the code tags when writing code... Its terribly distressing trying to read that.
[mod_edit: aye, added
]
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Sat May 17, 2003 7:45 pm
yes, of course. Just put the anchor tag around the text you want to act as link.
Although providing a copy&paste-able script is not good for the learning curve, take this as example
Code: Select all
<html>
<head>
<style type="text/css">
tr.even_row { background-color: white; }
tr.odd_row { background-color: #CCFFCC; }
</style>
</head>
<body>
<?php
$db =mysql_connect("localhost","jam", "");
mysql_select_db("jam_uk_db",$db);
$sql = 'SELECT * FROM anime';
$rs = mysql_query($sql,$db);
$numofrows = mysql_num_rows($rs);
?>
<table border="1">
<tr>
<th>Anime Name</th>
<th> </th>
</tr>
<?php
for ($x=0; $x != $numofrows; $x++)
{
$rs=mysql_query($sql,$db);
?>
<tr class="<?php echo ($x%2==0) ? 'even_row' : 'odd_row'; ?>">
<td>
<a href="detail.php?Anime_id=<?php echo $row['Anime_id']; ?>">
<?php echo $row['Anime_name']; ?>
</a>
</td>
<td>
<?php echo $row['Anime_id']; ?>
</td>
</tr>
<?php
}
?>
</table>
</body>
</html>(that's my style of coding, not necessarily the best one
and it's untested)
if you name the parameter
detail.php?Anime_id = you'll have to fetch $_GET['Anime_id'], of course
jamrop
Forum Commoner
Posts: 80 Joined: Fri May 16, 2003 5:38 pm
Post
by jamrop » Sat May 17, 2003 7:58 pm
hey thanks for ur help
i am a newbie
Can i ask one more question
if i wanted to added a image of the anime_name next to the details links
how would i go abuot doing that?
would it be something like
echo '<td><img scr=', $row['Anime_id'.jpg], '">
assumption: the image is in the main directory of the php files
Many thanks again
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Sun May 18, 2003 4:53 am
then you'd try to access an array element of $row that is named
'Anime_id'.jpg (the concatenation of
'Anime_id' and the
constant jpg
But you want a tag (as seen from the client)
<img src="N.jpg" />
where N is the value of
$row['Anime_id']
jamrop
Forum Commoner
Posts: 80 Joined: Fri May 16, 2003 5:38 pm
Post
by jamrop » Sun May 18, 2003 6:37 am
hey
so u r saying that u need to put
echo '<td><font size="-1">', $row['Anime_name'], '</td>';
echo '<td><font size="-1"><a href="detail.php?Anime_id=', $row['Anime_id'], '">Info</a></td>';
echo '<img src="$row['Anime_id'].jpg" /> ';
THanks for helping me out