Jump to content

How do I duplicate this code in a table to display other items


Recommended Posts

Hi,

I tried simply duplicating this, but it didn't work, so this code simply retrieves items for a shop, and adds a link, I just want to duplicate the link but for a different item, at the moment, all items will have the same link.

Obviously, the items listed, are from one table. So a new set of items would be from another.


# Retrieve items from 'shop' database table.
$q = "SELECT * FROM shop" ;
$r = mysqli_query( $dbc, $q ) ;
if ( mysqli_num_rows( $r ) > 0 )
{
  # Display body section.
  echo '<table><tr>';
  while ( $row = mysqli_fetch_array( $r, MYSQLI_ASSOC ))
  {
    echo '<td><strong>' . $row['item_name'] .'</strong><br><span style="font-size:smaller">'. $row['item_desc'] . '</span><br><img src='. $row['item_img'].'><br>$' . $row['item_price'] . '<br><a href="added.php?id='.$row['item_id'].'">Add To Cart</a></td>';
  }
  echo '</tr></table>';

echo '<table><tr>';
  while ( $row = mysqli_fetch_array( $r, MYSQLI_ASSOC ))
  {
    echo '<td><strong>' . $row['item_name'] .'</strong><br><span style="font-size:smaller">'. $row['item_desc'] . '</span><br><img src='. $row['item_img'].'><br>$' . $row['item_price'] . '<br><a href="added.php?id='.$row['item_id'].'">Add To Cart</a></td>';
  }
  echo '</tr></table>';
  
  # Close database connection.
  mysqli_close( $dbc ) ;

Thanks.

Link to post
Share on other sites

So this is one example there.

<?php
/* Open a connection */
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
# Retrieve items from 'shop' database table.
$q = "SELECT * FROM shop" ;
$r = mysqli_query( $dbc, $q ) ;
if ( mysqli_num_rows( $r ) > 0 )

    /* seek to row no. 400 */
    $result->data_seek(399);

    /* fetch row */
    $row = $result->fetch_row();

    printf ("City: %s  Countrycode: %s\n", $row[item_name], $row[item_desc]);

    /* free result set*/
    $result->close();
}

/* close connection */
$mysqli->close();
?>

I am not sure, I can use this, but put the values of the table?

printf ("City: %s  Countrycode: %s\n", $row[item_name], $row[item_desc]);

Link to post
Share on other sites

Don't copy the examples, just use the mysqli_data_seek function to reset the pointer to the first row of the results before looping through them a second time.

Link to post
Share on other sites
Posted (edited)

So replace one of the table codes with a mysqli_data_seek()

Or just keep the one.

# Retrieve items from 'shop' database table.
$q = "SELECT * FROM shop" ;
$r = mysqli_query( $dbc, $q ) ;
if ( mysqli_num_rows( $r ) > 0 )
{
  # Display body section.
  echo '<table><tr>';
  while ( $row = mysqli_fetch_array( $r, MYSQLI_ASSOC ))
  {
    echo '<td><strong>' . $row['item_name'] .'</strong><br><span style="font-size:smaller">'. $row['item_desc'] . '</span><br><img src='. $row['item_img'].'><br>$' . $row['item_price'] . '<br><a href="added.php?id='.$row['item_id'].'">Add To Cart</a></td>';
  }
  echo '</tr></table>';

echo '<table><tr>';
  while ( $row =  mysqli_data_seek( $r, MYSQLI_ASSOC ))
  
    echo '<td><strong>' . $row['item_name'] .'</strong><br><span style="font-size:smaller">'. $row['item_desc'] . '</span><br><img src='. $row['item_img'].'><br>$' . $row['item_price'] . '<br><a href="added.php?id='.$row['item_id'].'">Add To Cart</a></td>';
  }

 

 

 

 

Edited by Html
Link to post
Share on other sites

No, you are the first poster to bring that to my attention. I looked at the example, I thought the example there is the solution to this.

I think having two tables is probably not needed to begin with.

Link to post
Share on other sites

MySQLi gives you a result set, it is a bit like an array but you don't have direct access to it. The result set starts off with a pointer pointing to the first element in the array. Each call to mysqli_fetch_array() will return the element that the pointer is currently pointing to and then move the pointer to the next element in the result set. Once there are no more rows left in the result set, mysqli_fetch_array() returns false and leaves the pointer pointing to nothing.

The mysqli_data_seek() function lets you choose which element in the internal array to point to. Obviously, if you want to iterate through all the rows again, you will want to point to the first element again.

This should be all the information you need to solve your problem.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...