1

I currently have a problem with splitting data that is retrieved from database.

Example: when I display it, it shows data1 data2 data3 data4. But I want it to be displayed like

  • Data1 Data2
  • Data3 Data4

I have tried several ways to do it, but I failed to do so. Is there anything I have to change in my code in order to achieve that?

Here is my code:

<table class="table">

    function pingAddress($ip,$kiosk) {
                    $fp = @fSockOpen($ip,80,$errno,$errstr,1);
                    $checkCount = 0;                
                    if(is_resource($fp)){
                        if($fp) { 
                            $status=0; 
                            fclose($fp); 
                            echo '<td><p><b>'.$kiosk.'</b></p><img src="images/green.png" width=50px height=50px></img></td>';  
                            $checkCount++;

                        }
                    }
                    else{
                        echo '<td><p><b>'.$kiosk.'</b></p><img src="images/red.png" width=50px height=50px></img></td>';        
                        $checkCount++;

                    }
                    if($checkCount % 2 ==0){
                        echo '</table><table class="table">';
                    }

                }
                // Some IP Address
                pingAddress("192.168.1.0","Kuantan (35)"); 
                pingAddress("192.168.1.1","UTC Kuantan (36)"); 
                pingAddress("192.168.1.2","Temerloh (37)"); 
                pingAddress("192.168.1.3","Bentong (6)"); 

</table>

The result should be shown like this:

 - Kuantan 35 UTC Kuantan 36
 - Temerloh 37 Bentong 6
8
  • Just use <tr></tr> . Commented Apr 16, 2015 at 4:29
  • Display it in the <table></table> it will work for you. Commented Apr 16, 2015 at 4:30
  • At the first place, you might use ArrayChunk to slice your data into your size. Commented Apr 16, 2015 at 4:33
  • @Iffi May i know what do you mean by <table></table>? As i already have a table in there already why still need extra? Commented Apr 16, 2015 at 5:54
  • @MarcusTan you have already tables so just add <tr> tags .. Start new <tr> and at the end of function put condition if($checkCount == 2){ echo '</tr>' } Its mean close tr tag if output is display two time and than start tr new tag. Commented Apr 16, 2015 at 6:08

2 Answers 2

2

You has ignored a <tr> in your code, this split the data in rows to archieve your goal.

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

1 Comment

Hi there, i had try to put <tr> in my code but still the same result...it display in a line for the result where i wanted to split them into a new line after 2 data
1

I think there is problem with your code, because as I see you first close and after open table tag - it means in first time you call the function you'll close missing tag. After that if you like to show two results on the row, it means you have to add global variable ( or add another one external variable in call ) which rule where the current result will be shown - on first or second column. And on the end - I think it is bad practice to enclose single result on table tags - at least I can't see the meaning for it. Why don't you use for example?

Comments

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.