Jump to content

[SOLVED] array order


chocopi

Recommended Posts

if an array is not in numerical order is it in alphabetical order ?

 

EG:

 

In my database i have a, b and c stored. If i had an array like

 

$array = array('','aaa','bbb','ccc');

 

Would it put them in alphabetical order so a = aaa , b = bbb and c = ccc ?

 

Cheers

 

~ Chocopi

Link to comment
https://forums.phpfreaks.com/topic/53605-solved-array-order/
Share on other sites

Cheers Frost,

This is what I have so far, but I do not know how to put $table1 into the array.

 

<?php
$enemy_id = $_SESSION['enemy_id'];
$enemy_drop1 = $_SESSION['enemy_drop1'];
// Work out enemy drops
$enemy_query = mysql_query("SELECT `drop1` FROM `Enemies` WHERE `id`='$enemy_id'");
$row = @mysql_fetch_assoc($enemy_query) or die("Invalid query, please contact an administrator!");
list($table1, $id1) = split("_", $row['drop1']);
$array = array('','Body','Hats','Items','Pants','Weapons');
sort($array);
foreach ($array as $key => $array)
{
$drop1 = mysql_query("SELECT `$id1` FROM `$array` WHERE `id`='$id1'");
}
?>

 

Thanks,

 

~ Chocopi

Link to comment
https://forums.phpfreaks.com/topic/53605-solved-array-order/#findComment-265458
Share on other sites

        // this will not work as the $array has to stay intact.
foreach ($array as $key => $array)
{
$drop1 = mysql_query("SELECT `$id1` FROM `$array` WHERE `id`='$id1'");
}

       // try this instead
foreach ($array as $key => $table)
{
$drop1 = mysql_query("SELECT `$id1` FROM `$table` WHERE `id`='$id1'");
}

Link to comment
https://forums.phpfreaks.com/topic/53605-solved-array-order/#findComment-265534
Share on other sites

Lol  ;D

 

Ok but for some reason the query is empty this is what i have:

 

<?php
$enemy_id = $_SESSION['enemy_id'];
// Work out enemy drops
$enemy_query = mysql_query("SELECT `drop1` FROM `Enemies` WHERE `id`='$enemy_id'");
$row = @mysql_fetch_assoc($enemy_query) or die("Invalid query, please contact an administrator!");
list($table1, $id1) = split("_", $row['drop1']);
$array = array('','Body','Hats','Items','Pants','Weapons');
sort($array);
foreach ($array as $key => $table1)
{
$drop1 = mysql_query("SELECT `$id1` FROM `$array` WHERE `id`='$id1'");
mysql_query($drop1) or die(mysql_error());
}
?>

Link to comment
https://forums.phpfreaks.com/topic/53605-solved-array-order/#findComment-266910
Share on other sites

<?php
$enemy_id = $_SESSION['enemy_id'];
// Work out enemy drops
$enemy_query = mysql_query("SELECT `drop1` FROM `Enemies` WHERE `id`='$enemy_id'");
$row = @mysql_fetch_assoc($enemy_query) or die("Invalid query, please contact an administrator!");
list($table1, $id1) = split("_", $row['drop1']);
$array = array('','Body','Hats','Items','Pants','Weapons');
sort($array);
foreach ($array as $key => $table1)
{
$drop1 = mysql_query("SELECT `$id1` FROM `$table1` WHERE `id`='$id1'");
mysql_query($drop1) or die(mysql_error());
}
?>

 

I bet it was when you are trying to call the table with the value "Array" you needed to put $table1 not $array as was pointed out above...

Link to comment
https://forums.phpfreaks.com/topic/53605-solved-array-order/#findComment-266932
Share on other sites

blah my bad didn't notice that you are trying to do 2 query runs.

 

<?php
$enemy_id = $_SESSION['enemy_id'];
// Work out enemy drops
$enemy_query = mysql_query("SELECT `drop1` FROM `Enemies` WHERE `id`='$enemy_id'");
$row = @mysql_fetch_assoc($enemy_query) or die("Invalid query, please contact an administrator!");
list($table1, $id1) = split("_", $row['drop1']);
$array = array('','Body','Hats','Items','Pants','Weapons');
sort($array);
foreach ($array as $key => $table1)
{
$drop1 = "SELECT `$id1` FROM `$table1` WHERE `id`='$id1'"; 
mysql_query($drop1) or die(mysql_error() ' ON Query ' . $drop1); // you were trying to run a query that was already ran and returned a resource. once should do.
}
?>

 

Try that, sometimes it's hard to catch all the mistakes made.

Link to comment
https://forums.phpfreaks.com/topic/53605-solved-array-order/#findComment-266936
Share on other sites

cheers, i thought about adding the . but i decided against it  :-[

 

So im being told the table does not exist (Incorrect table name '' ON Query SELECT `` FROM `` WHERE `id`='' )

 

But the vaules in the array

$array = array('','Body','Hats','Items','Pants','Weapons');

 

are corrected so i dont have clue what the problem is

Link to comment
https://forums.phpfreaks.com/topic/53605-solved-array-order/#findComment-266963
Share on other sites

Note the first value of the array

 

$array = array('','Body','Hats','Items','Pants','Weapons');

 

it is ''

 

$array = array(Body','Hats','Items','Pants','Weapons');

 

That may throw it off.

 

$drop1 = "SELECT `$id1` FROM `$table1` WHERE `id`='$id1'";

 

Why are you trying to select a column $id1, which apparently has no value ???

 

$drop1 = "SELECT `id` FROM `$table1` WHERE `id`='$id1'";

 

maybe what you are looking for but that makes no sense what so ever.

 

$enemy_query = mysql_query("SELECT `drop1` FROM `Enemies` WHERE `id`='$enemy_id'") or DIE(mysql_error());

 

Change the above just incase that is not getting the data you want.

 

Other than that you are on your own bud. That is about all I can find wrong without seeing the DB structure etc.

Link to comment
https://forums.phpfreaks.com/topic/53605-solved-array-order/#findComment-266970
Share on other sites

Cheers i changed it to the following as suggested  ;D

 

Now echo $drop1; gives me the resource id so how i change this into the value. I tried $drop1 = $drop1['id']; but that didnt work

 

<?php
// Work out enemy drops
$enemy_query = mysql_query("SELECT `drop1` FROM `Enemies` WHERE `id`='$enemy_id'");
$row = @mysql_fetch_assoc($enemy_query) or die("Invalid query, please contact an administrator!");
list($table1, $id1) = split("_", $row['drop1']);
$array = array('Body','Hats','Items','Pants','Weapons');
sort($array);
foreach ($array as $key => $table1)
{
$drop1 = "SELECT `id` FROM `$table1` WHERE `id`='$id1'"; 
$drop1 = mysql_query($drop1) or die(mysql_error() . ' ON Query ' . $drop1);
}
?>

Link to comment
https://forums.phpfreaks.com/topic/53605-solved-array-order/#findComment-266982
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.