Click here to Skip to main content
15,896,153 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting problem in how to create discrete divs when accessing the function's returned value into a while loop, but I want that divs to be created one outside the other instead of being created one inside the other. See the below code:
HTML
<html>
<head>
<title>sdcsdzc</title>
</head>
<body>
<?php

$j=0;
while($j<=2)
{                    $removed=array("http://www.w3schools.com/html/default.asp","http://www.w3schools.com/js/default.asp","http://www.w3schools.com/sql/default.asp");
$data=crawl_page($removed[$j]);
echo "
<div style=' background-color: #E7EFEF;width:84%;border:1px solid #A5BEBE;'> 
<div style='background-color: white;border:1px solid #A5BEBE;'>
$data[0]
</div>
</div>";
$j++;
}

function crawl_page($b)
{ 
$a=fopen($b,"r");
$contents=stream_get_contents($a);
fclose($a);
$contents=strtolower($contents);

$start='<div class="example"';

$start_pos=strpos($contents,$start);
$first_trim=substr($contents,$start_pos);

$stop='</div>';
$stop_pos=strpos($first_trim,$stop);

$second_trim=substr($first_trim,0,$stop_pos);

$out['0'] = $second_trim;
return $out;
}
?>
</body>
</html>


I cant send you the image of my output but you can run this code directly in php and see the divs are creating inside the other, but I want them to come out discretely one after the other. If anyone can do this please do tell me, its important for me !!
Posted

1 solution

I guess, you missed some closing div, the reason why, your div's layout is breaking. Please check below code.

<html>
    <head>
        <title>sdcsdzc</title>
    </head>
    
    <body>
            function crawl_page($b) { 
            $a = fopen($b, "r");
            $contents = stream_get_contents($a);
            
            fclose($a);
            $contents1 = strtolower($contents);

            $start = '<div class="example"></div>';
            $stop_pos = strpos($first_trim,$stop);

            $second_trim = substr($first_trim,0,$stop_pos);

            $out['0'] = $second_trim;
            return $out;
        }
        
        $j = 0;
        while ($j <= 2) {                    
            $removed = array("http://www.w3schools.com/html/default.asp",
                             "http://www.w3schools.com/js/default.asp",
                             "http://www.w3schools.com/sql/default.asp");
            $data = crawl_page($removed[$j]);
            echo "<div style="background-color: #E7EFEF;width:84%;border:1px solid #A5BEBE; margin-bottom: 5px;">";
            echo "<div style="background-color: white;border:1px solid #A5BEBE;">";
            echo $data[0] . "</div></div>";
            $j++;
        }
    ?>
    </body>
</html>

Hope this one will solve your problem.
 
Share this answer
 
Comments
Member 11107959 29-Oct-14 1:12am    
no, its still not working but good try i dint think of that method .i've tried your code but in your code $data[0] is not fetching any data thats why divs showing nothing but they are continuous .$out is not returning anything
Their is one more thing i want to tell you that if i do write any text like "hello world"or"sometext" instead of $data[0] inside the 2nd div then it work properly it shows discrete divs but as i put $data[0] that contains web data then only divs disorder their positions
and i've also checked every tag i really cant find what am i missing if you can, give it a one more try its really important for me!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900