0

i want to make a table that on the first column consist of name from database and on the second column consist of value from an array. this is what i have

<table>
    <tr> 
        <?php $arr=array(3,5,2,4,1);                            
        $name= mysql_query("SELECT name FROM table")
        foreach ($arr as $rs){                          
            while($n=mysql_fetch_array($name)){ ?>                      
                <td><?=$n['name']?></td>
                <td><?=$rs?></td>
    </tr>
    <?php   }   }   ?>                          
</table>

i need an output like

name_a 3
name_b 5
name_c 2
name_d 4
name_e 1

thank you in advance.

1
  • 2
    as Rajdeep has shown, you dont need the foreach loop for this. In addition to that i wanted to tell you that you should try to use mysqli instead of mysql since mysql is deprecated. cheers Commented Jan 13, 2017 at 14:23

1 Answer 1

2

You don't need that foreach loop. Simply use array_shift() function inside while() loop to get each array element. So your code should be like this:

<table>
    <tr> 
    <?php 
        $arr=array(3,5,2,4,1);                            
        $name= mysql_query("SELECT name FROM table")                        
        while($n=mysql_fetch_array($name)){ 
            $rs = array_shift($arr); 
    ?>                      
            <td><?=$n['name']?></td>
            <td><?=$rs?></td>
    </tr>
    <?php   
        }      
    ?>                          
</table>

Furthermore, your business logic work will work only if the number of the rows in the table equals to the number of elements in the array. And if it's not the case then you need to use LIMIT clause in your query.


Sidenote: Don't use mysql_* functions, they are deprecated as of PHP 5.5 and are removed altogether in PHP 7.0. Use mysqli or pdo instead. And this is why you shouldn't use mysql_* functions.

Sign up to request clarification or add additional context in comments.

14 Comments

thank you for the answer. but. don't you have another way? because the actual array comes from a sorting process. when i use the array_shift. the array element comes back to before it's sorted.. do you understand what i mean? i'm not good when it comes to array thing.. sorry for troubling you..
i dont think i completely understand, but have you tried sorting the array before the while loop?
@ゆにたどうえ As @Cashbee said above ^, you need to sort the array before using it in while() loop. Or can you at least show the code where you're doing sorting.
@Cashbee this was my question yesterday rank the $arr comes from there.. when i use array_shit() i got the original array back..
@ゆにたどうえ That's probably because you're applying array_shift() function on $original array, not on $rank array. The correct statement would be like this: $rs = array_shift($rank);. Also, make sure you use this inside while loop.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.