zigojacko
zigojacko

Reputation: 2063

Trying to Output from MySQL in PHP to Divs

I'm trying to connect page management up to our database, so on the manage pages page, it needs to list all pages from the database, one on each row - for as many pages as there are (well, until I figure out how to add pagination - but one step at a time)!

Page is all connected to database.

This is what I have.

$query_all_pages = "SELECT * FROM `pages` WHERE parent_id = '0'";
$all_pages = mysql_query($query_all_pages, $con) or die(mysql_error());
$row_all_pages = mysql_fetch_assoc($all_pages);
$num_rows_all_pages = mysql_num_rows($all_pages);

The table which this data needs to be outputted to is made up of divs, see below:

<div class="table_row"> 
<div class="td_page_name"><p><?php echo $row_all_pages['page_display_name']; ?></p></div>   
<div class="td_page_title"><p>Title Here...</p></div>
<div class="td_page_type"><p>Type...</p></div>
<div class="td_page_view"><p>View</p></div>
<div class="td_page_edit"><p>Edit</p></div>
<div class="td_page_delete"><p>Delete</p></div>
</div>

As you can see, I've been attempting to display the list of pages in the appropriate column, but after an hour of messing around with various variations, I've got to admit, I don't really know what I'm doing here.

I'm not sure if I need to be inserting a while loop in PHP but if anyone could explain how I should be calling in the results into the appropriate column as per the above variables, I will be eternally grateful.

Any help, greatly appreciated. Thanks in advance :)

Upvotes: 0

Views: 1415

Answers (2)

Dan
Dan

Reputation: 526

<?php  
  $query = mysql_query("SELECT * FROM `pages` WHERE parent_id = '0'") or die(mysql_error());
    if(mysql_num_rows($query)<1) {
        echo "No results";
    }
    else {
        while($row = mysql_fetch_array($query)) { ?>
<div class="table_row"> 
<div class="td_page_name"><p><?php echo $row['page_display_name']; ?></p></div>   
<div class="td_page_title"><p>Title Here...</p></div>
<div class="td_page_type"><p>Type...</p></div>
<div class="td_page_view"><p>View</p></div>
<div class="td_page_edit"><p>Edit</p></div>
<div class="td_page_delete"><p>Delete</p></div>
</div>

        <?php }
    }
?>

Try using the mysql_fetch_array function

Additional:

If you wanted to check the length of your data (as you mentioned), you could write a custom function like so:

<?php
function checkLength($str) {
   if(strlen($str)<10) {
       return $str;
   }
   else {
       return substr($str,0,10);
   }
}
?>

And then in your existing code, call the checkLength() function like so:

<?php echo checkLength($row['page_display_name']); ?>

Should do the trick.

Upvotes: 2

ThePrimeagen
ThePrimeagen

Reputation: 4582

$dbRows = array();
while ($row = mysql_fetch_assoc($all_pages)) {
    $dbRows[] = $row;
}


Then once you have all the data just do this

<?php foreach($dbRows as $row) { ?>
    <div class="table_row"> 
        <div class="table_page_name"><p><?php echo $row['page_display_name']; ?></p></div>   
        <div class="table_title"><p><?php echo $row["title"]; ?></p></div>
        <div class="table_type"><p><?php echo $row["type"]; ?></p></div>
        <div class="table_view"><p><?php echo $row["view"]; ?></p></div>
        <div class="table_edit"><p><?php echo $row["edit"]; ?></p></div>
        <div class="table_delete"><p><?php echo $row["delete"]; ?></p></div>
    </div>
<?php } ?>

You get the idea.

Upvotes: 2

Related Questions