-2

screenshot hereI have 2 data entries in my "job" database and I want to display them as a list using for loop.

I can only see the first entry in a list and the loop is only works on the first entry.

  $select="SELECT * FROM job j JOIN jobtype jt ON j.jobtypeID = jt.jobtypeID JOIN location l
  ON j.locationID = l.locationID";

  $ret=mysqli_query($connection,$select);
  $count=mysqli_num_rows($ret);
  $row1=mysqli_fetch_array($ret);

<div class="mb-5">

<?php

  for ($i=0;$i<$count;$i++) 
  { 
    $arr=mysqli_fetch_array($ret);
    $jobTypeName = $row1['jobTypeName'];
    $jobTitle = $row1['jobTitle'];
    $locationName = $row1['locationName'];
    $salary = $row1['salary'];    
?>

  <div class="row align-items-start job-item border-bottom pb-3 mb-3 pt-3">
    <div class="col-md-4">
      <span class="badge badge-primary px-2 py-1 mb-3"> <?php echo $jobTypeName ?> </span>
      <h2>
        <?php echo $jobTitle ?>
      </h2>

    </div>
    <div class="col-md-3 text-left">
      <p class="meta"> <strong>Location</strong></p>
      <h3>
        <?php echo $locationName ?> </h3>

    </div>
    <div class="col-md-3 text-md-right">
      <p class="meta"> <strong>Salary</strong></p>
      <strong class="text-black"> <?php echo $salary ?> </strong>
    </div>

  </div>

<?php
  }
?>
</div>
Emma
  • 19
  • 1
  • 6
  • that is an unusual way to iterate through a recordset - I suspect that is the problem – Professor Abronsius Jun 25 '19 at 07:38
  • where are you getting **row** variable? –  Jun 25 '19 at 07:39
  • I have edited my code. – Emma Jun 25 '19 at 07:41
  • check with if statement for the count and also use while loop instead. – Ashwin Pandey Jun 25 '19 at 07:44
  • Each call to `mysqli_fetch_array` fetches a new row. So you need to call `mysqli_fetch_array` on each iteration of the loop (e.g. `while($row = mysqli_fetch_array($ret))`. Also, `mysqli_fetch_array` does not return an associative array. You want `mysqli_fetch_assoc` for that. – Cully Jun 25 '19 at 07:57
  • @Emerald I have added a new answer to the duplicate that I closed with to show you that you don't _need_ to call any `fetch()`ing functions to iterate your result set. I recommend that you switch to object oriented syntax -- I find it a better habit because its less redundant syntax is more brief and easier to read. – mickmackusa Jun 25 '19 at 10:00

4 Answers4

2

Calling mysqli_fetch_array initially as you did then again in the loop will consume the recordset ( 2 records ) on the first iteration of the loop - call it for each row

<div class="mb-5">

<?php
    while( $rs=mysqli_fetch_array( $ret, MYSQLI_ASSOC ) ){
        $jobTypeName = $rs['jobTypeName'];
        $jobTitle = $rs['jobTitle'];
        $locationName = $rs['locationName'];
        $salary = $rs['salary'];
?>

  <div class="row align-items-start job-item border-bottom pb-3 mb-3 pt-3">
    <div class="col-md-4">
      <span class="badge badge-primary px-2 py-1 mb-3"> <?php echo $jobTypeName ?> </span>
      <h2>
        <?php echo $jobTitle ?>
      </h2>
    </div>
    <div class="col-md-3 text-left">
      <p class="meta"> <strong>Location</strong></p>
      <h3><?php echo $locationName ?></h3>
    </div>
    <div class="col-md-3 text-md-right">
      <p class="meta"> <strong>Salary</strong></p>
      <strong class="text-black"> <?php echo $salary ?> </strong>
    </div>
  </div>    

<?php
    }//close while loop
?>
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
-1

You are reading from the DB into $arr, but showing data from $row1:

$arr = mysqli_fetch_array($ret);
$jobTypeName = $row1['jobTypeName'];

Try looping the query results.

t1gor
  • 1,244
  • 12
  • 25
  • This is only one of many problems in the OP's code. It doesn't answer the question. – Cully Jun 25 '19 at 07:41
  • The answer does not suppose that I'll post the code, isn't it supposed to be a hint in the right direction? – t1gor Jun 25 '19 at 07:44
  • If you have no intention of resolving the whole question, convert your post to Wiki to invite others to complete your answer. – mickmackusa Jun 25 '19 at 09:22
-1

What value does the variable $count hold?. it needs to contain the number of rows. you may use the mysqli_num_rows function to fetch the number of rows. for example: $count = mysqli_num_rows($ret).

Also $row1=mysqli_fetch_array($ret); should be removed. It is fetching a row from database without doing anything with it.

Inside the for loop, the code $arr=mysqli_fetch_array($ret); should be replaced with $row1=mysqli_fetch_array($ret);

Nadir Latif
  • 3,690
  • 1
  • 15
  • 24
  • I have already done it in my code but still not working properly. – Emma Jun 25 '19 at 07:44
  • Even if `$count` is set correctly, it can't be used to loop over the results. That's not how you iterate over results using `mysqli_fetch_array`. – Cully Jun 25 '19 at 07:44
-1
<?php  

  $select="SELECT * FROM job j JOIN jobtype jt ON j.jobtypeID = jt.jobtypeID JOIN location l
  ON j.locationID = l.locationID";

  $ret=mysqli_query($connection,$select);
  $count=mysqli_num_rows($ret);
  // $row1=mysqli_fetch_array($ret);

 ?>
    <div class="mb-5">
<?php

  $subselect="SELECT * FROM job j JOIN jobtype jt ON j.jobtypeID = jt.jobtypeID JOIN location l
  ON j.locationID = l.locationID";
$subret=mysqli_query($connection,$subselect);
$subcount=mysqli_num_rows($subret);

for($j=0;$j<$subcount;$j++)
{      
  $row1=mysqli_fetch_array($subret);
$jobTypeName =  $row1['jobTypeName'];
$jobTitle =   $row1['jobTitle'];
$locationName =   $row1['locationName'];
$salary = $row1['salary'];

?>

          <div class="row align-items-start job-item border-bottom pb-3 mb-3 pt-3">
            <div class="col-md-4">
              <span class="badge badge-primary px-2 py-1 mb-3"> <?php echo $jobTypeName ?> </span>
              <h2><?php echo $jobTitle ?></h2>

            </div>
            <div class="col-md-3 text-left">
                <p class="meta"> <strong>Location</strong></p>
              <h3> <?php echo $locationName ?> </h3>

            </div>
            <div class="col-md-3 text-md-right">
               <p class="meta"> <strong>Salary</strong></p>
              <strong class="text-black"> <?php echo $salary ?> </strong>
            </div>

           </div>

        <?php
      }
      ?>

 </div>
        </div>
Emma
  • 19
  • 1
  • 6
  • I have solved my question in this way and it works just like the way I wanted the result to be. Thank you everyone for helping me with different solutions. – Emma Jun 25 '19 at 08:13