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.

  • 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 – kscherrer Jan 13 '17 at 14:23

1 Answers1

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.

Community
  • 1
  • 1
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
  • 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.. – ゆにた どうえ Jan 13 '17 at 14:31
  • 2
    i dont think i completely understand, but have you tried sorting the array before the while loop? – kscherrer Jan 13 '17 at 14:54
  • 1
    @ゆにたどうえ 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. – Rajdeep Paul Jan 13 '17 at 14:59
  • @Cashbee this was my question yesterday [rank](http://stackoverflow.com/questions/41617333/how-to-rank-arrays-index-in-php) the $arr comes from there.. when i use array_shit() i got the original array back.. – ゆにた どうえ Jan 13 '17 at 15:08
  • @ゆにたどうえ So this array `$arr` is rank array or element array? Please clearly explain your problem i.e. *what you're trying to do* and *what is not working*. – Rajdeep Paul Jan 13 '17 at 15:16
  • @RajdeepPaul the rank array.. i want to write the name who have the element in the 1st column and than the rank. in the 2nd column – ゆにた どうえ Jan 13 '17 at 15:19
  • @ゆにたどうえ Your question is not very clear. This answer aligns with the output format given in the question. May I know what output format you're expecting? Is it different from what you've shown in the question? Also, From where you're getting *elements* array? If the *name*s constitutes *elements* array, then how you're planning to rank them since they are not simple integers? – Rajdeep Paul Jan 13 '17 at 15:41
  • @RajdeepPaul no no. That's exactly how I want it to be. I need to make it in a table with 2 columns. The 1st column is the name from database and the 2nd one is from the rank array.. – ゆにた どうえ Jan 13 '17 at 15:47
  • @ゆにたどうえ What's the current output now? And how is it different from the expected one? Post your *expected* and *current* output in [pastebin.com](http://pastebin.com/index.php) and give me it's link here. – Rajdeep Paul Jan 13 '17 at 15:48
  • @RajdeepPaul thank you for keeping up with me. [pastebin](http://pastebin.com/7Hp1dSiz) – ゆにた どうえ Jan 13 '17 at 16:14
  • @ゆにたどうえ That's probably because you're using `array_shift()` function **outside** of the `while` loop. Isn't it? – Rajdeep Paul Jan 13 '17 at 16:20
  • @RajdeepPaul oh. I'm sorry. that was without array_shift().. with the array_shift() it becomes [pastebin](http://pastebin.com/KBE4PqC0) – ゆにた どうえ Jan 13 '17 at 16:32
  • 1
    @ゆにたどうえ 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. – Rajdeep Paul Jan 13 '17 at 16:32
  • @RajdeepPaul omg.. it works now. i was out of focus. i'm so sorry for troubling you.. and thank you so much <3 <3 <3 <3 <3. – ゆにた どうえ Jan 13 '17 at 16:39