简体   繁体   English

我怎样才能在循环一遍又一遍地重复时停止 php?

[英]How can i stop a php while loop repeating over and over?

I have a polymer(1.0) app i am trying to create (based on stretch-tutorials league table), i cant figure out the routing if use mvc, so i have opted for the one page site, referencing different php files.我有一个正在尝试创建的聚合物(1.0)应用程序(基于拉伸教程排行榜),如果使用 mvc,我无法弄清楚路由,所以我选择了单页站点,引用不同的 php 文件。

I Have a table in MYSql that i trying to incorporate into the table, using a simple echo table script, but this repeats itself hundreds of times.我在 MYSql 中有一个表,我尝试使用简单的回显表脚本将其合并到表中,但这会重复数百次。 How can i stop this loop ?我怎样才能停止这个循环?

$result = mysql_query('select * from results',$connection) or die('cannot show tables');
while($tableName = mysql_fetch_row($result)) {

$sql = ('SELECT `home_team_name`,`away_team_name`,`home_goals_for`, `away_goals_for`, DATE_FORMAT(`fixture_date`, "%e %M %Y"), TIME_FORMAT(`fixture_time`, "%h:%i %p") FROM `results` ORDER BY fixture_date desc LIMIT 20  '.$table) or die('cannot show columns from '.$table);

echo '<h3>',$table,'</h3>';
$result2 = mysql_query($sql);
if(mysql_num_rows($result2)) {
    echo '<table cellpadding="4" cellspacing="0" class="table table-striped table-hover table-bordered">';
    echo '<tr class="info"><th>Home team</th><th>Away Team</th><th>Score</th><th>Score</th><th>Date<th>Time</th></tr>';
    while($row2 = mysql_fetch_row($result2)) {
        echo '<tr>';
        foreach($row2 as $key=>$value)  {
            echo '<td>',$value,'</td>';
        }
        echo '</tr>';
    }
    echo '</table><br />';
}
  }

I have added ' < 50 ' but this returns 50 table header rows only ?我添加了“<50”,但这仅返回 50 个表标题行?

the site is live at http://jay-o.uk/#!/results the css and other data are okay for now, just this pesky loop.该站点位于http://jay-o.uk/#!/results 上,css 和其他数据目前还可以,只是这个讨厌的循环。

You need to set a "LIMIT" in the first query if you want to avoid a timeout operation, also i think that is posible to call a unique query that return all the info that you need but i don't know because your query is unintelligible.如果你想避免超时操作,你需要在第一个查询中设置一个“LIMIT”,我也认为可以调用一个唯一的查询来返回你需要的所有信息,但我不知道,因为你的查询是无法理解。

Your code is wrong, the second query don't use the $table variable that is null value and what is de purpose to putting it after the limit parameter?您的代码是错误的,第二个查询不使用为空值的 $table 变量,将它放在 limit 参数之后的目的是什么?

$results = mysql_query('SELECT * FROM table_name ORDER BY field_name LIMIT 50');
if(mysql_num_rows($results)) {
  print '<table>';
  while ($row = mysql_fetch_row($results)) {
    print '<tr>';
    foreach ($row as $field_name => $field_value) print '<td>'.$field_value.'</td>';
    print '</tr>'
  }
  print '</table>';
}

Sorry, the code is a bit messy, ive been working on this for a while and ifs frustrating, im not sure where i am going wrong, if i remove the $tableName variable i get an empty array, no matter what i do this is the results.抱歉,代码有点乱,我已经研究了一段时间,如果令人沮丧,我不确定我哪里出错了,如果我删除 $tableName 变量,我会得到一个空数组,无论我做什么,这是结果。

I have tried in json to no avail...我在 json 中尝试过无济于事...

$db = mysql_connect($host,$user,$pass);

if (!$db) {

    die('Could not connect to db: ' . mysql_error());

}



//Select the Database

mysql_select_db($db_name,$db);

$resulting = mysql_query("select * from results", $db); 

//Create an array

$json_response = array();

while ($row = mysql_fetch_array($resulting, MYSQL_ASSOC)) {

    $row_array['home_team_name'] = $row['home_team_name'];

    $row_array['away_team_name'] = $row['away_team_name'];

    $row_array['home_goals_for'] = $row['home_goals_for'];

    $row_array['away_goals_for'] = $row['away_goals_for'];

    //push the values in the array

    array_push($json_response,$row_array);

   $json = json_encode($json_response);


//echo $json;


$json_result = print_r((array) json_decode($json,true));

echo $json_result;

}

which leaves this: jay-o.uk/config/config.php剩下这个:jay-o.uk/config/config.php

Could this be because i am referring to a view in mySql rather than a table ?这可能是因为我指的是 mySql 中的视图而不是表吗?

Well.. maybe you need this code:好吧..也许你需要这个代码:

// Create an array
$items = array();

while ($row = mysql_fetch_array($resulting, MYSQL_ASSOC)) {
    /* print new insert */
    var_dump('new item');

    /* add a new item */
    $items[] = array(
        'home_team_name' => $row['home_team_name'],
        'away_team_name' => $row['away_team_name'],
        'home_goals_for' => $row['home_goals_for'],
        'away_goals_for' => $row['away_goals_for']
    );
}

$json_response = json_encode($items);
print $json_response;

If $json_response are a empty array maybe the problem is that the query don't return any row.如果 $json_response 是一个空数组,问题可能在于查询不返回任何行。

一个更简单的解决方案如何,只需将来自数据库的查询限制为 1:

    $results = mysql_query('SELECT * FROM table_name ORDER BY field_name LIMIT 1');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM