Jump to content

MySQL & Array Help


stig1

Recommended Posts

Wondering how I would go about creating a nice script which would do the following:

 

Gather all the data from one column of a database for multiple rows, for example:

 

nobody, sign, word, framed, frame, fire, text, house, flames, signage, burning

nobody, spring, seasonal, holiday, season, colored eggs, young, chicks, colorful, occasion, Easter, rabbit

 

Put the above into an one array with each value a new key in the array. (Please note, there would be alot more than 2 rows in the database table). I would assume you have to explode the comma out to put each word into a new key in the array.

 

Array to look like the following:

 

nobody

sign

seasonal

word

framed

fire

nobody

etc...

 

Secondly I would like to remove all the duplicate values from the array.

 

Then I would print the array out into a combo box.

 

Is this possible? If so, how would i go about doing it?

 

Huge thankyou in advance.

Link to comment
https://forums.phpfreaks.com/topic/166723-mysql-array-help/
Share on other sites

it would be something like this

 

(wrote in on the fly .. very untested or proof read, but should give you an idea)

<?php
$conn = mysql_connect("localhost", "mysql_user", "mysql_password");
if (!mysql_select_db("mydbname")) {
    echo "Unable to select mydbname: " . mysql_error();
    exit;
}

$sql = "SELECT nobody, sign, word, framed, frame, fire, text, house, flames, signage, burning
nobody, spring, seasonal, holiday, season, colored eggs, young, chicks, colorful, occasion, Easter, rabbit FROM sometable";

$result = mysql_query($sql);
$data = array();
while ($row = mysql_fetch_assoc($result)) {
    $data[] = $row;
}
mysql_free_result($result);

$data = array_unique($data);

//okay putting the data in a combo box like this is kinda dumb but that what you asked for!
echo "<select>";
//okay adding all values from all keys!
foreach($data as $d) //keys
{
  foreach($d as $K => $V) //values
  {
    echo "<option value=\"$K\">$V</option>";
  }
}
echo "</select>";
?>

Link to comment
https://forums.phpfreaks.com/topic/166723-mysql-array-help/#findComment-879172
Share on other sites

the column name in the database is just keywords, not all the other information, that is actual sample data.

 

nobody, sign, word, framed, frame, fire, text, house, flames, signage, burning

nobody, spring, seasonal, holiday, season, colored eggs, young, chicks, colorful, occasion, Easter, rabbit

 

ive done this, except it still gives me a duplicate values of the word nobody

 

$data = '';

 

$a = "SELECT keywords FROM library";

$a_result = $DB_MYSQL->QUERY($a);

 

while ($b = mysql_fetch_array($a_result)){

 

$data .= $b['keywords'].',';

 

}

 

echo $data;

 

echo "<br /><br />";

 

 

$keywords = explode(',',$data);

 

print_r($keywords);

 

echo "<br /><br />";

 

$unique = array_unique($keywords);

 

print_r($unique);

 

Link to comment
https://forums.phpfreaks.com/topic/166723-mysql-array-help/#findComment-879176
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.