miligraf Posted October 2, 2007 Share Posted October 2, 2007 I have a field that has only 4 different information, lets say that the field's name is "color" and it has 4 different information that are: red, blue, green and yellow. How can i get those 4 or more colors out of the field with PHP? Thanks! Link to comment https://forums.phpfreaks.com/topic/71481-get-the-different-information-in-a-field/ Share on other sites More sharing options...
Psycho Posted October 2, 2007 Share Posted October 2, 2007 YOu need to be more specific. Is this a text field where the user has entered four values? Are they separated by a delimiter such as a comma? Is it a select field? If so, you will probably need some javascript with a hidden field to capture all the values into PHP. Specifics please. Link to comment https://forums.phpfreaks.com/topic/71481-get-the-different-information-in-a-field/#findComment-359836 Share on other sites More sharing options...
MadTechie Posted October 2, 2007 Share Posted October 2, 2007 non-specific question, soooo try echo $_POST['color']; or echo $_GET['color']; Link to comment https://forums.phpfreaks.com/topic/71481-get-the-different-information-in-a-field/#findComment-359841 Share on other sites More sharing options...
miligraf Posted October 2, 2007 Author Share Posted October 2, 2007 ok i was not so specific, sorry Heres an example with a table: id-----------name-------------thing----------color 01----------Jones-------------car------------red 02----------Al-----------------house---------blue 03----------Cher--------------bottle----------yellow 04----------Santana-----------cat------------red 05----------Maggie------------bicycle---------green 06----------Homer-------------plane-----------yellow How can i get the info in the "color" field like this: red, blue, yellow, green. (with the colors not being repeated) Instead of getting each "color" of each "id": red, blue, yellow, red, green, yellow. I hope you understand me, thanks. Link to comment https://forums.phpfreaks.com/topic/71481-get-the-different-information-in-a-field/#findComment-359861 Share on other sites More sharing options...
MadTechie Posted October 2, 2007 Share Posted October 2, 2007 Sorry still not 100% sure what your asking! you asking about a database (if so what database) or a form! for a mysql database you could use something like this! <?php $conn = mysql_connect("localhost", "mysql_user", "mysql_password"); if (!$conn) { echo "Unable to connect to DB: " . mysql_error(); exit; } if (!mysql_select_db("mydbname")) { echo "Unable to select mydbname: " . mysql_error(); exit; } $sql = "SELECT * FROM thetable"; //or //$sql = "SELECT * FROM thetable WHERE ID = 1"; //or //$sql = "SELECT * FROM thetable WHERE color = 'red' "; //note the single quotes $result = mysql_query($sql); if (!$result) { echo "Could not successfully run query ($sql) from DB: " . mysql_error(); exit; } if (mysql_num_rows($result) == 0) { echo "No rows found, nothing to print so am exiting"; exit; } while ($row = mysql_fetch_assoc($result)) { echo $row["id"]; echo "="; echo $row["color"]; echo "<br>"; } mysql_free_result($result); ?> Link to comment https://forums.phpfreaks.com/topic/71481-get-the-different-information-in-a-field/#findComment-359867 Share on other sites More sharing options...
Psycho Posted October 2, 2007 Share Posted October 2, 2007 I think this is what you want (assuing we are talking about a MySQL DB). This query will return one record for each unique color in the table SELECT DISTINCT color FROM tableName Link to comment https://forums.phpfreaks.com/topic/71481-get-the-different-information-in-a-field/#findComment-360080 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.