如何只显示$ classname一次

时间:2016-11-16 06:03:32

标签: php mysqli

我试图只回显一次$ classname。 所以它就像这样显示。

小狗

第一次等等等等

第二名

额外第3次

目前显示如下:

小狗

第一次等等等等

小狗

第二名

小狗

额外第3次



@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);          
    setContentView(R.layout.activity_main);
    Toolbar mToolBar = (Toolbar) findViewById(R.id.app_bar);

    setSupportActionBar(mToolBar);
    getSupportActionBar().setTitle("Some titile);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

       mToolBar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            /**
             change code here*/
        }
    });
}




4 个答案:

答案 0 :(得分:1)

使用counter检查是否已显示:

$ctr = 0;
while{
  $classname = $row['class_name'];

  if($ctr == 0){
    echo $classname;
    $ctr++;
  }

  //display the rest
  ...
}

答案 1 :(得分:0)

您可以在while循环中按索引保持跟踪记录,如:

<?php

            // SO UPDATE THE QUERY TO ONLY PULL THAT SHOW'S DOGS
            $query = "SELECT c.* , p.* FROM result c,dogs p WHERE c.dog_id=p.dog_id";


            $result = mysqli_query($connection, $query) or trigger_error
            ("Query Failed! SQL: $query - Error: ". mysqli_error
            ($connection), E_USER_ERROR);

            if ($result) {
            $i = 1;
            while ($row = mysqli_fetch_assoc($result)) {
            $placement = $row['placement'];
            $classname = $row['class_name'];
            $dog_name = $row['dog_name'];
            $award = $row['award'];
            ?>  

            <table>
                <tr>
                <td><strong><?php if($i==1) { echo $classname; } ?></strong> </td><br>
                </tr>
                <tr>
                <td><strong><?php echo $placement, $award ?></strong>  <?php echo $dog_name ?></td> 
                </tr>
            </table>








            <?php $i++;  }}} ?>

答案 2 :(得分:0)

所以,不知道你为什么在while循环中使用<table>,这将根据你的行没有打印。

以下是基本示例,您可以将值存储在数组中,而不是将其与HTML一起使用:

示例:

<?php
if ($result) {
  $myarr = array();
  while ($row = mysqli_fetch_assoc($result)) {
    $myarr[$row['class_name']][] = $row;  //store values into an array against each class in group
  }
}

foreach ($myarr as $key => $value) {
    echo "Class Name: ". $key."<br/>"; // will print class name
    foreach ($value as $fvalue) {
        echo "Placement: ".$row['placement']."<br/>";; // placement
        echo "Dog Name: ".$row['dog_name']."<br/>"; // dog name
        echo "Award: ".$row['award']."<br/>";; // award
    }
}
?>

其他解决方案是在while循环中使用增量变量,如其他答案中所述。

答案 3 :(得分:0)

  

如果您希望只有一个表,那么打开和关闭<Table>标签应该在您的Loop 之外。如果您希望包含2个单元格的行(如下面的代码所示),即使<tr>代码也应包含<td>代码:

<?php

    // SO UPDATE THE QUERY TO ONLY PULL THAT SHOW'S DOGS
    $query      = "SELECT c.* , p.* FROM result c,dogs p WHERE c.dog_id=p.dog_id";
    $result     = mysqli_query  ($connection, $query) or trigger_error
                                ("Query Failed! SQL: $query - Error: ".
                                mysqli_error($connection), E_USER_ERROR);
    ?>

    <table>

    <?php
    if ($result) {
        while ($row = mysqli_fetch_assoc($result)) {
            $placement  = $row['placement'];
            $classname  = $row['class_name'];
            $dog_name   = $row['dog_name'];
            $award      = $row['award'];

    ?>
        <tr>
            <td>
                <strong><?php echo $classname ?></strong> 
                <!-- DO YOU NEED THIS <BR>TAG HERE? <br> -->
            </td>
            <td>
                <strong><?php echo $placement, $award ?></strong><?php echo $dog_name ?>
            </td>
        </tr>
        <!-- EXCEPT IF YOU WISH TO HAVE ONE COLUMN, THE ROW BELOW IS UNNECESSARY -->
        <!--
        <tr>
            <td>
              <strong><?php echo $placement, $award ?></strong>  <?php echo $dog_name ?>
            </td>
        </tr>
        -->

    <?php 
        }   // CLOSE THE WHILE LOOP;
        }   // CLOSE THE IF STATMENT;
    ?>
</table>