简体   繁体   中英

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.

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. 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 ?

the site is live at http://jay-o.uk/#!/results the css and other data are okay for now, just this pesky loop.

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.

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?

$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.

I have tried in json to no avail...

$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

Could this be because i am referring to a view in mySql rather than a table ?

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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