Mysql fetch array, table results

后端 未结 6 1809
名媛妹妹
名媛妹妹 2021-01-14 10:07

i\'m pretty new to this and not sure how should i do it,

I\'ve got a database with a column called \"names\"

how can i make it display in so?



        
相关标签:
6条回答
  • 2021-01-14 10:21

    So most of these answers are going for the kludge. First, if all you want is the name in your resultset, then only ask for it. So rather than going with:

    $result = mysql_query('SELECT * FROM member');
    

    Instead go with:

    $result = mysql_query('SELECT names FROM member');
    

    Also, everyone seems to be ignoring the three column request, and that can be satisfied using a modulo to tell when to break the rows. We'll do a little magic to make sure you always close the row tag.

    $row_count = 0;
    while($row = mysql_fetch_array($result))
      {
      if( $row_count % 3 == 0 )
        {
           echo '<tr>';
        }
        echo '<td width="270px">'.$row['names'].'</td>';
      if( $row_count % 3 == 0 )
        {
           echo '</tr>';
        }
      $row_count++; 
      }
    

    I don't want to get too picky about your schema, but if you have a choice, it's better to name the table a plural like members rather than member, since you have a collection of rows, each one representing one 'member'. And unless your members have multiple names that column could probably best be called 'name' instead.

    0 讨论(0)
  • 2021-01-14 10:22

    You know accomplish this by assigning the row count before you start the variable and few flags to know initialized loop or started/ended loop.

    $i = 1;
    $initFlag = false;
    $flag = "closed";
    while($row = mysql_fetch_array($result)) {
        if($i%3 == 0) $initFlag = false;
        if($flag == "closed" && ($i == 1 || $i % 3 == 1)) { 
            echo "<tr>"; $flag = "started"; $initFlag = true; 
        }
    
        echo '<td width="270px">'.$row['names'].'</td>';
    
        if(!initFlag && $flag == "started" && $i % 3 ==0) { 
            echo "</tr>"; $flag = "closed"; 
        }
    
    
        $i++;
    }
    
    0 讨论(0)
  • 2021-01-14 10:25

    Lets say you have an array of the names called $names, then you could do what you wanted with code that looks something like this:

    <tr>
    <?php
    
    foreach($names as $name) {
        print "<td>$name</td>";
    }
    ?>
    </tr>
    

    That would put all the names on the same row.

    In order to start a new row say, every 3 names, you could put an if statement in the for loop like this:

    // assume we have these variables available.
    $row_number;
    $max_cols = 3;
    
    // this goes at the top of the foreach
    if($row_number % $max_cols == 0) {
        print '</tr><tr>';
    }
    
    0 讨论(0)
  • 2021-01-14 10:27
    $result = mysql_query('SELECT * FROM member');
      echo'<tr>'
      while($row = mysql_fetch_array($result))
      {
    
      echo '<td width="270px">'.$row['names'].'</td>';
      }
    
      echo '</tr>';
    
    0 讨论(0)
  • 2021-01-14 10:33

    $row will update on each iteration of the loop:

    $result = mysql_query('SELECT * FROM member');
    
    echo '<tr>';
    
    for($i = 0; $row = mysql_fetch_array($result); $i = ($i+1)%3){
        echo '<td width="270px">'.$row['names'].'</td>';
        if($i == 2)
            echo '</tr><tr>';
    }
    
    echo '</tr>';
    
    0 讨论(0)
  • 2021-01-14 10:34
    $result = mysql_query('SELECT * FROM member');
    
    echo '<tr>;
    
    while($row = mysql_fetch_array($result))
    {
    
        echo '<td width="270px">'.$row['names'].'</td>';
    }
    
    echo '</tr>';
    
    0 讨论(0)
提交回复
热议问题