doufei1988 2014-10-22 05:14
浏览 48
已采纳

从循环外定义的函数传递变量但在循环内调用

i want to call a function which is defined outside the loop but called inside the loop and then access a variable from that function definition which is outside the loop to inside the loop every time loop runs
here is the database connectivity that contains while loop and a funtion calling and then accessing the $second_trim variable defined in ""function page($h)"" inside the while loop

  <html>
  <head>
  <title></title>
 </head>     
 <body>
 <?php
 $getquery = mysql_query("SELECT * FROM table WHERE $construct LIMIT $start, $per_page");
 while($runrows = mysql_fetch_assoc($getquery))
 {
 $title = $runrows ['Title'];
 $url = $runrows ['URL'];
 page($url);
 echo "<div style='clear: both;position:relative;border:1px solid #A5BEBE;background-color:   white;'>
 $second_trim</div>";
}
function page($h){
$i=fopen($h,"r");
$contents=stream_get_contents($i);
fclose($i);
$contents=strtolower($contents);

$start='<div';

$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);
}
?>
</body>
</html>

the while loop fetch url from the database and pass through "page($url)" function called inside the loop to defined function "function page($h)"outside the loop it will get required content in $second_trim that will displayed inside the loop in echo statement every time it runs but i m getting the error like Fatal error: Call to undefined function crawl_page() their is some syntax problem i m facing that i cant find after alot of search is their any can do this plz help me out

  • 写回答

1条回答 默认 最新

  • dongxing7083 2014-10-22 05:23
    关注

    You need to define your function -> function page($h){...} before your loop. Second, return the $second_trim at the end of your function, and third set your function call to your variable

    ...your html...
    <?php
    
    function page($h){
          $i=fopen($h,"r");
          $contents=stream_get_contents($i);
          fclose($i);
          $contents=strtolower($contents);
          $start='<div';
          $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);
    
          //return the value
          return $second_trim;
    }
    
    
    $getquery = mysql_query("SELECT * FROM table WHERE $construct LIMIT $start, $per_page");
    while($runrows = mysql_fetch_assoc($getquery)){
          $title = $runrows ['Title'];
          $url = $runrows ['URL'];
    
          //save the function returned value to variable
          $second_trim = page($url);
    
          echo "<div style='clear: both;position:relative;border:1px solid #A5BEBE;background-color:   white;'>
          $second_trim</div>";
    }
    ?>
    ...your html...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 深度学习残差模块模型
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)