Jump to content

From SQL to Array?


godsent

Recommended Posts

I've been thinking how can i get information from sql table into array. My code looks like this:

 

function loadMenu() {
$menu_name = array("Home", "Register", "Login", "test" );
$menu_locations = array("?home", "?register", "?login", "?test");
$menu_count = sizeof($menu_name);

print "<li class='first'><a href='$menu_locations[0]'> $menu_name[0] </a></li>";
for ($i = 1; $i < $menu_count; ++$i) {
print "<li><a href='$menu_locations[$i]'> $menu_name[$i]</a></li>";
  }
}

 

and I'm wondering how can i get "Home, Register, Login, Test" from SQL into array $menu_name.

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/136662-from-sql-to-array/
Share on other sites

Well I'd expect you'd have some idea of what you were adding to your database, but like this;

 

Lets imagine your table was;

id INT(11) auto-increment Primary Key

number TINYINT(2)

 

$yourArray = array(1,2,3,4,5,6,7);
$query = "INSERT INTO `yourtable` (`number`) VALUES ";
for($i=1;$i<=count($yourArray);$i++){
$query .= "({$yourArray[$i]}), ";
}
$query = substr($query,0,-2);
$result = mysql_query($query) or die(mysql_error());

 

 

Please note I haven't included any validation or database connection

Link to comment
https://forums.phpfreaks.com/topic/136662-from-sql-to-array/#findComment-713800
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.