Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

PHP/MySQL include

Original Message
Name: mikeatvillage
Date: May 23, 2007 at 01:52:12 Pacific
Subject: PHP/MySQL include
OS: Win2k
CPU/Ram: 2gb/512mb
Comment:
Hi, I currently have this line in one of my php pages:-

<td>
<?php include("inc/movementsin.php");?>
</td>

inc/movementsin.php contains:-

<?php
print"<SELECT NAME=\"movementin\">
<OPTION VALUE=\"$row[movementin]\" SELECTED>$row[movementin]
<OPTION VALUE=\"Bought\">Bought
<OPTION VALUE=\"Born\">Born
<OPTION VALUE=\"Found\">Found
<OPTION VALUE=\"Borrowed\">Borrowed
</SELECT>";
?>

What I want to do now is to replace this with the appropriate code so instead of taking the values from this "hard coded" list is to take the values from a table (already set up and only has 1 field "flocknumbers") which contains the values.

So, basically it's :-

<td>
<?php include "table-flocknumbers";?>
</td>

How do I code this please ?

Thanks

Mike



Report Offensive Message For Removal


Response Number 1
Name: Michael J (by mjdamato)
Date: May 23, 2007 at 10:51:01 Pacific
Subject: PHP/MySQL include
Reply: (edit)
<?php

//Connect to mysql
$db = mysql_connect("localhost", "username", "password") or die(mysql_error());

//Select the database
mysql_select_db("databasename",$db) or die(mysql_error());

//Run query
$query = "SELECT * FROM table-flocknumbers";
$result = mysql_query($query) or die(mysql_error());

//Determine if there were any results
if (!mysql_num_rows($result)) {

echo "There were no results.";

} else { //There were results

//Create the select list
echo "<SELECT NAME=\"movementin\">\n";

//Iterate through the results and create the options
while ($row = mysql_fetch_assoc($result)) {

echo "<OPTION VALUE=\"$row[flocknumbers]\">$row[flocknumbers]</option>\n";

}

//Close the select list
echo "</SELECT>\n";

}

?>

Michael J


Report Offensive Follow Up For Removal

Response Number 2
Name: Don Arnett
Date: May 23, 2007 at 10:52:20 Pacific
Subject: PHP/MySQL include
Reply: (edit)
-self delete-

Michael J beat me to the punch, plus his code is more specific to the question.


Report Offensive Follow Up For Removal

Response Number 3
Name: mikeatvillage
Date: May 24, 2007 at 03:47:47 Pacific
Subject: PHP/MySQL include
Reply: (edit)
Thanks both,

I'll give that a try later and let you know. I think I can strip some line out as I should already be connected to the db/host and I know there will be results.

Mike


Report Offensive Follow Up For Removal

Response Number 4
Name: mikeatvillage
Date: May 24, 2007 at 04:10:58 Pacific
Subject: PHP/MySQL include
Reply: (edit)
Perfect, I've stripped it down to this. Thanks again.

<?php
include ("inc/table.php");
$query = "SELECT * FROM flock_numbers";
$result = mysql_query($query) or die(mysql_error());
echo "<SELECT NAME=\"flock_no\">\n";
while ($row = mysql_fetch_assoc($result)) {
echo "<OPTION VALUE=\"$row[flock_no]\">$row[flock_no]</option>\n";
}
echo "</SELECT>\n";
?>


Report Offensive Follow Up For Removal

Response Number 5
Name: mikeatvillage
Date: May 24, 2007 at 04:28:30 Pacific
Subject: PHP/MySQL include
Reply: (edit)
Hi again, another question..

I've replaced the SELECT to look at the main table and extract DISTINCT flock_no... rather than keeping them in a separate table.

$query = "SELECT DISTINCT(flock_no) FROM flock";

That works great and gives me a list of Flock Numbers which I already have used in the main table.

My next question is... is there a way to add a new Flock Number using this/similar code?

Years ago I used MS Access (yeuch) to do something similar - there was a "Limit to List = No" option if I recall.


Report Offensive Follow Up For Removal


Response Number 6
Name: Michael J (by mjdamato)
Date: May 24, 2007 at 07:35:34 Pacific
Subject: PHP/MySQL include
Reply: (edit)
If I unedrstand you correctly you are wanting a select list where the user can add a value on-the-fly by typing it in to the select list. That is not possible with any HTML technology.

Well, not without a LOT of work-arounds. There was a javascript I saw one that combined a select list and a text field to give that functionality - but it cost money.

Here is a possible solution. 1) Create a text box with the name that the select list has now and change the select list name to something else. The page receiving the form will get the value from the text box. Then use an onchange event on the select list and use it to populate the text box. Then add an option to the end of the selefct list for "Other".

When the user selects any value except other, the value is populated into the text field and the text field is hidden. When the user selects "Other" the field is emptied and becomes visible.

Here's an example:
<html>
<head>

<script type="text/javascript">

function setFlockNo(selValue) {

document.getElementById('flock_no').value = selValue;
displayVal = (selValue!='')?'none':'';
document.getElementById('flock_no_span').style.display = displayVal;

}

</script>

</head>
<body>

<form name="test">
Flock No:
<select name="flock_no_select" onchange="setFlockNo(this.value)">
<option value="Option A from DB">Option A from DB</option>
<option value="Option B from DB">Option B from DB</option>
<option value="Option C from DB">Option C from DB</option>
<option value="">Other</option>
</select>

<span style="display:none;" id="flock_no_span">
<input type="text" name="flock_no" id="flock_no">
</span>
</form>

</body>
</html>


Michael J


Report Offensive Follow Up For Removal

Response Number 7
Name: mikeatvillage
Date: May 24, 2007 at 08:11:23 Pacific
Subject: PHP/MySQL include
Reply: (edit)
Thanks Michael J,

I thought it would be a bit awkward - if not impossible. I'll keep a copy of the code you've supplied and look at it at a later date as I'm just beginning to learn the basics of PHP/MySQL at the moment.

Thanks again for all your assistance with this.

Mike


Report Offensive Follow Up For Removal

Response Number 8
Name: mikeatvillage
Date: June 4, 2007 at 04:13:51 Pacific
Subject: PHP/MySQL include
Reply: (edit)
Hi, me again (struggling as usual), I have another...

I've created this to extract data from a table and display all in a dropdown box. I want to use this to only add the selected "id" into a field named "parent_id"

So, which bit do I have to change ?

Many thanks.

<?php

$query = "SELECT id, petname, flock_no, ind_no FROM flock ORDER BY petname";

$result = mysql_query($query) or die(mysql_error());

echo "<SELECT NAME=\"id\",\"petname\",\"flock_no\",\"ind_no\">\n";

while ($row = mysql_fetch_assoc($result)) {

echo "<OPTION VALUE=\"id\",\"$row[petname]\",\"flock_no\">$row[id] $row[petname] ($row[flock_no]-$row[ind_no])</option>\n";
}

echo "</SELECT>\n";
?>


Report Offensive Follow Up For Removal

Response Number 9
Name: Michael J (by mjdamato)
Date: June 7, 2007 at 07:53:10 Pacific
Subject: PHP/MySQL include
Reply: (edit)
First of all, you should make new posts for new issues.

As to the code above and your query, I do not understand.

If you have a select field with the IDs why do you need to populate the selected ID into another field? Also your select field and option statements above has a very malformed name. What are you trying to accomplish by having comma separated values as the name/values? If it works at all it will only recognise the first value for each.

Michael J


Report Offensive Follow Up For Removal



Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: PHP/MySQL include

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




DSHUB24 Connection Problems

need help with dsl and dial up

novel 3.12

help mandriva install last straw!

Icon Scaling in Explorer Bar


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC