Okay, so I have successfully created a database that is full of information, and a way to display it in a table list. I have also created a link where if you click on it, it will show the character_id of the link.
rpgforumtest.freeiz.com/database.php
For example, Roronoa Zoro has a link, and if you click on it, it will send you to rpgforumtest.freeisz.com/view_character.php?character_id=2.
My problem is, retrieving information from the hyperlink? Do I use the php variable $_REQUEST?
Also, if anyone wants a test account to my forum, then use this:
Account name: Test
password: test1234
EDIT/
tried rewriting my question. Idk if it makes sense.
<?php
$character = isset($_GET['character_id']) ? (int) $_GET['character_id'] : 0;
That's the short way of writing "if 'character_id' is in the URL, cast that to an integer and use it, otherwise use 0"
Don't erase questions if you figure out the answer. It wastes everyone else's time: those who took the time to answer you, and those who take the time to read the topic. If you leave the original question (unless it's HIGHLY inappropriate), others can learn from the Q&A.
Okay, maybe on second thought I DO need help.
So now I'm able to make the links, and call a request for the links, as well as get some information from the link. Now I just need help querying more info from the database. The basic idea is once you click the link, you see more info. How do I query the database so I can view all information that a single row has?
For example, if the character_id is 2, then how do I query all the information out of that row within the database?
EDIT/
Also, I will put back my question.
$_REQUEST should work with either GET or POST transactions. $_GET could be used if character_id is definitely going to come in only in a URL query string, as opposed to a form POST.
Getting data from the database is a whole 'nuther kettle of fish. Read up on the SMF calls for making database queries (assuming this is an SMF thing you're working on). If character_id is unique to one record in this table, you'll get one record back in the form of an associative array (named elements), one per SELECT item. Please look through the SMF code for some examples of doing this.
Lainaus käyttäjältä: The Craw - heinäkuu 30, 2013, 12:40:45 IP
<?php
$character = isset($_GET['character_id']) ? (int) $_GET['character_id'] : 0;
That's the short way of writing "if 'character_id' is in the URL, cast that to an integer and use it, otherwise use 0"
It's better to use !empty() there, because character_id= (empty space) can happen and so can just character_id. If either is the case it'll go back to 0, without spitting errors (which would happen with isset).
Okay, here's my code so far.
if($_REQUEST['character_id'])
{
$character = isset($_GET['character_id']) ? (int) $_GET['character_id'] : 0;
$result = $smcFunc['db_query']('', 'SELECT * FROM {db_prefix}rpg_characters WHERE character_id = {string:charid}', array( 'charid' => '$character'));
while ($row = $smcFunc['db_fetch_assoc']($result));
echo $row['f_name'];
echo "<br>";
}
However, when it echoes, it doesn't display anything. Any suggestions?
Something like this should work, assuming that the page is connected with SMF somehow. If this is not an SMF thing, then check out PDO over at the PHP manual.
<?php
// Globalize $smcFunc so we can use it.
global $smcFunc, $context;
// Get the ID from the URL and store it in the $character_id variable.
$character_id = isset($_GET['character_id']) ? (int) $_GET['character_id'] : 0;
// Define the query string.
$query = '
SELECT *
FROM {db_prefix}rpg_characters
WHERE character_id = {int:id}';
// Define the parameters.
$parameters = array(
'id' => $character_id
);
// Get the result.
$result = $smcFunc('', $sql, $parameters);
// Grab the row and get it's information.
$context['rpg_member_values'] = array();
while ($row = $smcFunc['db_fetch_row']($result)) {
$context['rpg_member_values'] = $row[0];
}
// Output the response.
echo '<pre>', print_r($context['rpg_member_values']), '</pre>';
Also, I'm hoping you didn't really make your character ID column a string..
Lainaus käyttäjältä: Yoshi - heinäkuu 30, 2013, 02:09:50 IP
Lainaus käyttäjältä: The Craw - heinäkuu 30, 2013, 12:40:45 IP
<?php
$character = isset($_GET['character_id']) ? (int) $_GET['character_id'] : 0;
That's the short way of writing "if 'character_id' is in the URL, cast that to an integer and use it, otherwise use 0"
It's better to use !empty() there, because character_id= (empty space) can happen and so can just character_id. If either is the case it'll go back to 0, without spitting errors (which would happen with isset).
Makes no difference if you cast it as an integer, because any string that isn't a number and isn't "empty" will be set to 1, otherwise it's a zero. No error should occur no matter what $_GET['character_id'] is or is not set to.
I tried both string and int, but I was unsuccessful with both. Thank you, the Craw. I will give this a try and post my results when I am done.
*BUMP*
I inserted the code into my page however, when I ran it, this error occured.
Lainaa
Fatal error: Function name must be a string in /home/a3789387/public_html/view_character.php on line 146
on line 146 this is what it reads:
$result = $smcFunc('', $sql, $parameters);
It says that function name must be a string though I am not entirely sure if I should edit $result or $smcFunc, nor how it should be done.
You should use $smcFunc['db_query'] instead.
Whoops, thought I fixed that. Meh..
Entered the code with the correct function and now I get this
Please try again. If you come back to this error screen, report the error to an administrator.
I know the code isn't suppose to do it for me however, every time I tweak it, I get nothing but errors. I only wish to get all of the information in the table when the id equal 1,2,3 etc. So if the row(rows are horizontal, right?) is 2, I want to be able to pull all of the information in variables. EX: $row['f_name'], $row['class'], $row['job'].
Okay, let's stop guessing now. Please show us the entire script and all pertinent code, as well as the database table structure (preferably in SQL, but a screenshot of the table in PHPMyAdmin would also do).
Also, is this code in a custom SMF action, or an outside script with SSI.php?
This code is an outside script with require("SSI.php") before the <html>
This is the code for my view_character.php (this is where the query belongs)
<?php require("SSI.php"); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Rpg Forum Test - Viewing Character</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<meta name="description" content="Rpg Forum Central">
<link rel="stylesheet" type="text/css" href="http://rpgforumtest.freeiz.com/Themes/Default%20Copy/css/index.css?fin20" />
</head>
<body>
<div id="wrapper" style="width: 800px">
<div id="header">
<div class="frame">
<div id="top_section">
<h1 class="forumtitle">
<a href="http://rpgforumtest.freeiz.com/index.php">Rpg Forum Test</a>
</h1>
</div>
<div id="upper_section" class="middletext">
<div class="user">
<ul class="reset">
<li class="greetings"><span><?php ssi_welcome(); ?></span></li>
<li><a href='http://rpgforumtest.freeiz.com/index.php?action=unread'>Show unread posts since last visit.</a></li>
<li><a href='http://rpgforumtest.freeiz.com/index.php?action=unreadreplies'>Show new replies to your posts.</a></li>
</ul>
</div>
<div class="news normaltext">
<?php ssi_quickSearch(); ?>
<h2>News:</h2>
<?php ssi_news(); ?>
</div>
<br class="clear">
<br class="clear">
<br class="clear">
<br class="clear">
<?php ssi_menubar(); ?>
<br>
</div>
</div>
</div>
<div id="content_section">
<div class="frame">
<div id="main_content_section">
<div class="navigate_section">
<ul>
<li class="last">
<a href="http://rpgforumtest.freeiz.com/rpgc.php"><span>Roleplay Central</span></a>
»
<a href="http://rpgforumtest.freeiz.com/database.php"><span>Character Database</span></a>
»
<a href="http://rpgforumtest.freeiz.com/view_character.php"><span>Viewing Character</span></a>
</li>
</ul>
</div>
<div id="boardindex_table">
<table class="table_list">
<tbody class="header" id="category_1">
<tr>
<td colspan="4">
<div class="cat_bar">
<h3 class="catbg">
<center><a href="http://rpgforumtest.freeiz.com/database.php">Character Database</a></center>
</h3>
</div>
</td>
</tr>
</tbody>
<tbody class="content" id="category_1_boards">
<tr id="board_1" class="windowbg2">
<td style="width: 160px; border: 1px solid #000;">
<br class="clear">
<p><strong><a href="http://rpgforumtest.freeiz.com/rpgc.php">Roleplay Central</a></strong></p>
<br class="clear">
<a href="http://rpgforumtest.freeiz.com/database.php"><strong>Character Database</strong></a>
<br class="clear">
<li><a href="http://rpgforumtest.freeiz.com/create_character.php">Create a Character</a></li>
<li><a href="http://rpgforumtest.freeiz.com/edit_character.php">Edit Characters</a></li>
<li><a href="http://rpgforumtest.freeiz.com/adopt_character.php">Character Adoption</a></li>
<li><a href="http://rpgforumtest.freeiz.com/graveyard.php">Character Graveyard</a></li>
<br class="clear">
<a href="http://rpgforumtest.freeiz.com/shop.php"><strong>Goldhaven Marketplace</strong></a>
<br class="clear">
<li><a href="http://rpgforumtest.freeiz.com/buy_items.php">Buy Items</a></li>
<li><a href="http://rpgforumtest.freeiz.com/sell_items.php">Sell Items</a></li>
<li><a href="http://rpgforumtest.freeiz.com/trade_items.php">Trade Items</a></li>
<li><a href="http://rpgforumtest.freeiz.com/enchant_items.php">Enchant Items</a></li>
<li><a href="http://rpgforumtest.freeiz.com/upgrade_items.php">Upgrade Items</a></li>
<br class="clear">
<a href="http://rpgforumtest.freeiz.com/guilds.php"><strong>Guild HQ</strong></a>
<br class="clear">
<li><a href="http://rpgforumtest.freeiz.com/browse_guilds.php">Browse Guilds</a></li>
<li><a href="http://rpgforumtest.freeiz.com/guildranks.php">Guild Rankings</a></li>
<br class="clear">
<a href="http://rpgforumtest.freeiz.com/inn.php"><strong>Moriarty's Tavern</strong></a>
<br class="clear">
<li><a href="http://rpgforumtest.freeiz.com/heal.php">Lodging</a></li>
<li><a href="http://rpgforumtest.freeiz.com/patronize.php">Buy a Drink</a></li>
<li><a href="http://rpgforumtest.freeiz.com/patronize2.php">Rumors</a></li>
<br class="clear">
<a href="http://rpgforumtest.freeiz.com/coliseum.php"><strong>Champion's Coliseum</strong></a>
<br class="clear">
<li><a href="http://rpgforumtest.freeiz.com/battle_arena.php">Battle Arena</a></li>
<li><a href="http://rpgforumtest.freeiz.com/guildwars.php">Guild Wars</a></li>
<li><a href="http://rpgforumtest.freeiz.com/active_battles.php">Active Battles</a></li>
<li><a href="http://rpgforumtest.freeiz.com/top_factions">Top Factions</a></li>
<li><a href="http://rpgforumtest.freeiz.com/top_players">Top Players</a></li>
<br class="clear">
<a href="http://rpgforumtest.freeiz.com/bank.php"><strong>Bank of Phantasmia</strong></a>
<br class="clear">
<li><a href="http://rpgforumtest.freeiz.com/deposit.php">Deposit Gold</a></li>
<li><a href="http://rpgforumtest.freeiz.com/withdraw.php">Withdraw Gold</a></li>
<li><a href="http://rpgforumtest.freeiz.com/storage.php">Item Storage</a></li>
<br class="clear">
</td>
<td style="width: 600px; border: 1px solid #000;">
<center>
<?php
$allowed_groups = array(1, 2, 3, 4, 5, 6);
$can_see = FALSE;
foreach ($allowed_groups as $allowed)
if (in_array($allowed, $user_info['groups']))
{
$can_see = TRUE;
break;
}
if ($can_see)
{
if($_REQUEST['character_id'])
{
global $smcFunc, $context;
// Get the ID from the URL and store it in the $character_id variable.
$character_id = isset($_GET['character_id']) ? (int) $_GET['character_id'] : 0;
// Define the query string.
$query = '
SELECT *
FROM {db_prefix}rpg_characters
WHERE character_id = {int:id}';
// Define the parameters.
$parameters = array(
'id' => $character_id
);
// Get the result.
$result = $smcFunc['db_query']('', $sql, $parameters);
// Grab the row and get it's information.
$context['rpg_member_values'] = array();
while ($row = $smcFunc['db_fetch_row']($result)) {
$context['rpg_member_values'] = $row[0];
}
// Output the response.
echo '<pre>', print_r($context['rpg_member_values']), '</pre>';
}
}
?>
</center>
</td>
</tr>
</tbody>
<tbody class="divider">
<tr>
<td colspan="4"></td>
</tr>
</tbody>
</table>
</div>
<span class="clear upperframe">
<span></span>
</span>
<div class="roundframe">
<div class="innerframe">
<div class="cat_bar">
<h3 class="catbg">
Rpg Forum Test - Info Center
</h3>
</div>
<div id="upshrinkHeaderIC">
<div class="title_barIC">
<h4 class="titlebg">
<a href="http://rpgforumtest.freeiz.com/index.php?action=stats"><img class="icon" src="http://rpgforumtest.freeiz.com/Themes/Default Copy/images/icons/info.gif" alt="Forum Stats"></a> Recent Posts
</h4>
</div>
<p>
<?php ssi_recentPosts(); ?>
</p>
</div>
<div class="title_barIC">
<h4 class="titlebg">
<span class="ie6_header floatleft">
<a href="http://rpgforumtest.freeiz.com/index.php?action=who"><img class="icon" src="http://rpgforumtest.freeiz.com/Themes/Default Copy/images/icons/online.gif" alt="Users Online"></a>
Users Online
</span>
</h4>
</div>
<p><b>Top Poster</b>: <?php ssi_topPoster(); ?><br>
<?php ssi_logOnline(); ?></p>
<span class="lowerframe">
<span> </span>
</span>
</div>
</div>
</div>
<div id="footer_section">
<div class="frame">
</div>
</div>
</div>
</body>
</html>
This is is the script for database.php, which is where we start off. I'm not sure it's relavent but hey. Basically, you start off with the database.php where you find a character and then you click on the link and you get sent to view_character.php where it will display the rest of the character's information in a display table.
<?php require("SSI.php"); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Rpg Forum Test - Character Database</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<meta name="description" content="Rpg Forum Central">
<link rel="stylesheet" type="text/css" href="http://rpgforumtest.freeiz.com/Themes/Default%20Copy/css/index.css?fin20" />
</head>
<body>
<div id="wrapper" style="width: 800px">
<div id="header">
<div class="frame">
<div id="top_section">
<h1 class="forumtitle">
<a href="http://rpgforumtest.freeiz.com/index.php">Rpg Forum Test</a>
</h1>
</div>
<div id="upper_section" class="middletext">
<div class="user">
<ul class="reset">
<li class="greetings"><span><?php ssi_welcome(); ?></span></li>
<li><a href='http://rpgforumtest.freeiz.com/index.php?action=unread'>Show unread posts since last visit.</a></li>
<li><a href='http://rpgforumtest.freeiz.com/index.php?action=unreadreplies'>Show new replies to your posts.</a></li>
</ul>
</div>
<div class="news normaltext">
<?php ssi_quickSearch(); ?>
<h2>News:</h2>
<?php ssi_news(); ?>
</div>
<br class="clear">
<br class="clear">
<br class="clear">
<br class="clear">
<?php ssi_menubar(); ?>
<br>
</div>
</div>
</div>
<div id="content_section">
<div class="frame">
<div id="main_content_section">
<div class="navigate_section">
<ul>
<li class="last">
<a href="http://rpgforumtest.freeiz.com/rpgc.php"><span>Roleplay Central</span></a>
»
<a href="http://rpgforumtest.freeiz.com/database.php"><span>Character Database</span></a>
</li>
</ul>
</div>
<div id="boardindex_table">
<table class="table_list">
<tbody class="header" id="category_1">
<tr>
<td colspan="4">
<div class="cat_bar">
<h3 class="catbg">
<center><a href="http://rpgforumtest.freeiz.com/database.php">Character Database</a></center>
</h3>
</div>
</td>
</tr>
</tbody>
<tbody class="content" id="category_1_boards">
<tr id="board_1" class="windowbg2">
<td style="width: 160px; border: 1px solid #000;">
<br class="clear">
<p><strong><a href="http://rpgforumtest.freeiz.com/rpgc.php">Roleplay Central</a></strong></p>
<br class="clear">
<strong>Character Database</strong>
<br class="clear">
<li><a href="http://rpgforumtest.freeiz.com/create_character.php">Create a Character</a></li>
<li><a href="http://rpgforumtest.freeiz.com/edit_character.php">Edit Characters</a></li>
<li><a href="http://rpgforumtest.freeiz.com/adopt_character.php">Character Adoption</a></li>
<li><a href="http://rpgforumtest.freeiz.com/graveyard.php">Character Graveyard</a></li>
<br class="clear">
<a href="http://rpgforumtest.freeiz.com/shop.php"><strong>Goldhaven Marketplace</strong></a>
<br class="clear">
<li><a href="http://rpgforumtest.freeiz.com/buy_items.php">Buy Items</a></li>
<li><a href="http://rpgforumtest.freeiz.com/sell_items.php">Sell Items</a></li>
<li><a href="http://rpgforumtest.freeiz.com/trade_items.php">Trade Items</a></li>
<li><a href="http://rpgforumtest.freeiz.com/enchant_items.php">Enchant Items</a></li>
<li><a href="http://rpgforumtest.freeiz.com/upgrade_items.php">Upgrade Items</a></li>
<br class="clear">
<a href="http://rpgforumtest.freeiz.com/guilds.php"><strong>Guild HQ</strong></a>
<br class="clear">
<li><a href="http://rpgforumtest.freeiz.com/browse_guilds.php">Browse Guilds</a></li>
<li><a href="http://rpgforumtest.freeiz.com/guildranks.php">Guild Rankings</a></li>
<br class="clear">
<a href="http://rpgforumtest.freeiz.com/inn.php"><strong>Moriarty's Tavern</strong></a>
<br class="clear">
<li><a href="http://rpgforumtest.freeiz.com/heal.php">Lodging</a></li>
<li><a href="http://rpgforumtest.freeiz.com/patronize.php">Buy a Drink</a></li>
<li><a href="http://rpgforumtest.freeiz.com/patronize2.php">Rumors</a></li>
<br class="clear">
<a href="http://rpgforumtest.freeiz.com/coliseum.php"><strong>Champion's Coliseum</strong></a>
<br class="clear">
<li><a href="http://rpgforumtest.freeiz.com/battle_arena.php">Battle Arena</a></li>
<li><a href="http://rpgforumtest.freeiz.com/guildwars.php">Guild Wars</a></li>
<li><a href="http://rpgforumtest.freeiz.com/active_battles.php">Active Battles</a></li>
<li><a href="http://rpgforumtest.freeiz.com/top_factions">Top Factions</a></li>
<li><a href="http://rpgforumtest.freeiz.com/top_players">Top Players</a></li>
<br class="clear">
<a href="http://rpgforumtest.freeiz.com/bank.php"><strong>Bank of Phantasmia</strong></a>
<br class="clear">
<li><a href="http://rpgforumtest.freeiz.com/deposit.php">Deposit Gold</a></li>
<li><a href="http://rpgforumtest.freeiz.com/withdraw.php">Withdraw Gold</a></li>
<li><a href="http://rpgforumtest.freeiz.com/storage.php">Item Storage</a></li>
<br class="clear">
</td>
<td style="width: 600px; border: 1px solid #000;">
<center>
<hr>
<?php
$allowed_groups = array(1, 2, 3, 4, 5, 6);
$can_see = FALSE;
foreach ($allowed_groups as $allowed)
if (in_array($allowed, $user_info['groups']))
{
$can_see = TRUE;
break;
}
if ($can_see)
{
echo "Welcome to the Character Database. Here you can search the archives for all active characters in the game. For any characters that were killed off or died of old age, please check the <a href='http://rpgforumtest.freeiz.com/graveyard.php'>Character Graveyard.</a> Keep in mind that Adoptable Characters are not placed on this list.";
$result = $smcFunc['db_query']('', '
SELECT * FROM {db_prefix}rpg_characters ORDER BY l_name DESC',
array(
)
);
echo "<table border='1' style='border: 1px solid #000'>
<tr>
<th>Name</th>
<th>Gender</th>
<th>Age</th>
<th>Race</th>
<th>Class</th>
<th>Faction</th>
</tr>";
while($row = $smcFunc['db_fetch_assoc']($result))
{
echo "<tr>";
echo "<td><a href='http://rpgforumtest.freeiz.com/view_character.php?character_id=";
echo $row['character_id'];
echo "'>" . $row['l_name'];
echo " " . $row['m_name'];
echo " " . $row['f_name'] . "</a></td>";
echo "<td>" . $row['gender'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['race'] . "</td>";
echo "<td>" . $row['class'] . "</td>";
echo "<td>" . $row['faction'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
else
{
echo "You must login before viewing the content of this forum's features.";
ssi_login();
}
?>
</center>
<hr>
</td>
</tr>
</tbody>
<tbody class="divider">
<tr>
<td colspan="4"></td>
</tr>
</tbody>
</table>
</div>
<span class="clear upperframe">
<span></span>
</span>
<div class="roundframe">
<div class="innerframe">
<div class="cat_bar">
<h3 class="catbg">
Rpg Forum Test - Info Center
</h3>
</div>
<div id="upshrinkHeaderIC">
<div class="title_barIC">
<h4 class="titlebg">
<a href="http://rpgforumtest.freeiz.com/index.php?action=stats"><img class="icon" src="http://rpgforumtest.freeiz.com/Themes/Default Copy/images/icons/info.gif" alt="Forum Stats"></a> Recent Posts
</h4>
</div>
<p>
<?php ssi_recentPosts(); ?>
</p>
</div>
<div class="title_barIC">
<h4 class="titlebg">
<span class="ie6_header floatleft">
<a href="http://rpgforumtest.freeiz.com/index.php?action=who"><img class="icon" src="http://rpgforumtest.freeiz.com/Themes/Default Copy/images/icons/online.gif" alt="Users Online"></a>
Users Online
</span>
</h4>
</div>
<p><b>Top Poster</b>: <?php ssi_topPoster(); ?><br>
<?php ssi_logOnline(); ?></p>
<span class="lowerframe">
<span> </span>
</span>
</div>
</div>
</div>
<div id="footer_section">
<div class="frame">
</div>
</div>
</div>
</body>
</html>
screenshot of database
(https://www.simplemachines.org/community/proxy.php?request=http%3A%2F%2Fi49.photobucket.com%2Falbums%2Ff255%2FZeke1124%2Frpg-characters-table_zps417f2b59.png&hash=f57ea9a8f30d836732678bca691c11106eb70ea3)