...

View Full Version : Getting Info From A Table And Put Into Select Fields



CoolAsCarlito
07-28-2008, 09:33 PM
Can someone go to kansasoutlawwrestling.com/admin6.php and then click on create title. Then look in the main content frame. My question is how can I get the champion and contenders to be drop down select fields and show all the active wrestlers in a table I have called "wrestlers" or whatever I named it.

derzok
07-28-2008, 10:11 PM
echo '<select>';
$data = mysql_query("SELECT id_column, name_column FROM wrestlers");
while($row = mysql_fetch_assoc($data)) {
echo '<option value="'.$row['id_column'].'">'.$row['name_column'].'</option>';
}
echo '</select>';


You might have to do some checks on the incoming data to make sure that none of the people have to wrestle themselves. I don't know much about the sport, but I don't think you can do that. If you do it with AJAX you could make it so that it adds a "WHERE id_column != 'id_from_first_select_field'" after the first select has been chosen - so that it doesn't show people who've already been selected in the following selects.

CoolAsCarlito
07-28-2008, 10:24 PM
Thanks however I'm confused where do I put that at though. Do I put that in my code.

derzok
07-28-2008, 10:28 PM
Instead of the html that shows the text field (<input type=....>), insert the code I posted above (with the correct column names, of course). If it's inside of html, you'll need to wrap it in <?php ?> tags so that it's:


some html goes here
<?php
My code goes here
?>
More html goes here


Instead of:



Some html goes here
<input type="text" name="champion" />
the rest of the html here

CoolAsCarlito
07-28-2008, 10:30 PM
Wow that just goes to show I'm not thinking straight right now because I am still tired but thank you even though I did know that just not concentrating

ShaneC
07-28-2008, 10:36 PM
I would recommend you implement templates instead of PHP intertwined with HTML, though. Check out this thread (http://www.codingforums.com/showthread.php?p=709796) for more info on that.

CoolAsCarlito
07-28-2008, 10:36 PM
Well shoot now I get this:

Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/content/y/a/n/yankeefaninkc/html/addtitle.php on line 64



<?php
// Connects to your Database
$link = mysql_connect("?", "?", "?") or die(mysql_error());
mysql_select_db("?",$link) or die(mysql_error());

if (!mysql_select_db("?", $link)) {
echo 'Could not select database';
exit;
}

//This code runs if the form has been submitted
if (isset($_POST['addtitle'])) {

//This makes sure they did not leave any fields blank
if (!$_POST['titlename'] || !$_POST['type'] || !$_POST['status'] || !$_POST['champion'] || !$_POST['contender1'] || !$_POST['contender2'] || !$_POST['contender3'] ) {
die('You did not complete all of the required fields');
}

// checks if the title is in use
if (!get_magic_quotes_gpc()) {
$_POST['titlename'] = addslashes($_POST['titlename']);
}
$titlecheck = $_POST['titlename'];
$check = mysql_query("SELECT titlename FROM titles WHERE titlename = '$titlecheck'")
or die(mysql_error());
$check2 = mysql_num_rows($check);

//if the title exists it gives an error
if ($check2 != 0) {
die('Sorry, the title '.$_POST['titlename'].' is already in use.');
}


// now we insert it into the database
$insert = "INSERT INTO titles (titlename, type, status, champion, contender1, contender2, contender3)
VALUES ('".$_POST['titlename']."','".$_POST['type']."','".$_POST['status']."','".$_POST['champion']."','".$_POST['contender1']."','".$_POST['contender2']."','".$_POST['contender3']."')";
$add_title = mysql_query($insert,$link) or die(mysql_error());
?>


<h1>Title is created!</h1>

<?php
}
else
{
?>
<center><table border=1 cellpadding=5 cellspacing=0 width=350>
<font color="#CC0000"><h2><center>Create a Title</h2></center></font>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<tr><td>Title Name:</td><td>
<input type="text" name="titlename" maxlength="60">
</td></tr>
<tr><td>Type:</td><td>
<select name="type">
<option>Singles</option><option>Tag Team</option><option>Stable</option></select>
</td></tr>
<tr><td>Status:</td><td>
<select name="status">
<option>Active</option><option>Inactive</option></select>
</td></tr>
<tr><td>Champion:</td><td>
<?php
echo '<select>';
$data = mysql_query('SELECT * FROM singles');
while($row = mysql_fetch_assoc($data)) {
echo '<option value="'.$row['wrestlingname'].'">'.$row['wrestlingname'].'</option>';
}
echo '</select>';
?>
</td></tr>
<tr><td>Contender #1:</td><td>
<?php
echo '<select>';
$data = mysql_query("SELECT * FROM singles");
while($row = mysql_fetch_assoc($data)) {
echo '<option value="'.$row['wrestlingname'].'">'.$row['wrestlingname'].'</option>';
}
echo '</select>';
?>
</td></tr>
<tr><td>Contender #2:</td><td>
<?php
echo '<select>';
$data = mysql_query("SELECT * FROM singles");
while($row = mysql_fetch_assoc($data)) {
echo '<option value="'.$row['wrestlingname'].'">'.$row['wrestlingname'].'</option>';
}
echo '</select>';
?>
<tr><td>Contender #3:</td><td>
<?php
echo '<select>';
$data = mysql_query("SELECT * FROM singles");
while($row = mysql_fetch_assoc($data)) {
echo '<option value="'.$row['wrestlingname'].'">'.$row['wrestlingname'].'</option>';
}
echo '</select>';
?>
<tr><th colspan=2><input type="submit" name="createtitle" value="Create Title"></th></tr> </table>
</form>

<?php
}
?>

derzok
07-28-2008, 10:45 PM
I would recommend you implement templates instead of PHP intertwined with HTML, though. Check out this thread (http://www.codingforums.com/showthread.php?p=709796) for more info on that.

As much as I like using templates, they can get messy. For beginners I think it's best to just use php to process and print html. I recently made a site that's fully templated and object oriented. I had to produce a table, and man was it messy. Here's the code: http://zb3.zoklet.net/stats2/code.html - check out table.php and try not to cry :p

CoolAsCarlito
07-28-2008, 10:57 PM
Can someone tell me why I'm getting that error though?

derzok
07-28-2008, 11:17 PM
Interesting, that code runs fine on my machine. Usually that error means that you're missing a ; on or before the line number given. Are you sure you're running the correct/current version of the code? I often times resolve bugs in my code by actually uploading the code that I'm making changes to after changing it :p - we're all guitly of doing that at least once.

Line 64 is the first "echo" command of that block of code I gave you (which looks perfectly fine to me). So unless I'm missing something entirely obvious here, I think you're running the wrong code...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum