Jump to content

[SOLVED] Help changing a variable.


tqla

Recommended Posts

In the code below there are two sections - Categories (top) and Content (btm). In the Content part there is a column called Category. That number matches the actual ID in the Categories section above it.

 

Is there some php code that I can use to change that number to state the category Title from the Category section above it? Something to look at the category column in the Content (btm) section ( $row['category_id'] ) and match it to the ID of the Categories (top) section ($row2['id']) and then replace that number with the Title ($row2['title'])?

 

<?php
include('includes/connnection.inc.php');
$sql = 'SELECT * FROM content ORDER BY created DESC';
$result = mysql_query($sql) or die(mysql_error());
$sql2 = 'SELECT * FROM category ORDER BY title ASC';
$result2 = mysql_query($sql2) or die(mysql_error());
?>
<body>
<h1>Categories</h1>

<table cellspacing="10">
    <tr>
        <th align="left">Title</th>
      <th> </th>
      <th> </th>
    </tr>
   <?php
   while($row2 = mysql_fetch_assoc($result2)) {
   ?>

    <tr>
        <td align="left"><?php echo $row2['title']; ?></td>
        <td> </td>
    </tr>
   <?php } ?>
</table>

<h1>Content</h1>

<table cellspacing="10">
    <tr>
        <th align="left">Created</th>
        <th> </th>
        <th align="left">Updated</th>
        <th> </th>
        <th align="left">Title</th>
      <th> </th>
        <th align="left">Category</th>
      <th> </th>
        <th align="left">Active</th>
      <th> </th>
      <th> </th>
    </tr>
   <?php
   while($row = mysql_fetch_assoc($result)) {
   $row['created'] = date("m/d/y g:ia", strtotime($row['created']));
   $row['updated'] = date("m/d/y g:ia", strtotime($row['updated']));


   if ( $row['active'] == true ) {
      $active = "Yes";
   } else {
   $active = "No";
   }


   ?>

    <tr>
        <td align="left"><?php echo $row['created']; ?></td>
        <td> </td>
        <td align="left"><?php echo $row['updated']; ?></td>
        <td> </td>
        <td align="left"><?php echo $row['title']; ?></td>
        <td> </td>
        <td align="left"><?php echo $row['category_id']; ?></td>
        <td> </td>
        <td align="left"><?php echo $active; ?></td>
        <td> </td>

    </tr>


   <?php } ?>
</table>
</body>

Link to comment
https://forums.phpfreaks.com/topic/138656-solved-help-changing-a-variable/
Share on other sites

If you are trying to display the name of the category for a given item in your content, the way this would normally be achieved is by writing an SQL statement that lifts all your content items, with a JOIN on the categories table via Category Id.  Then every time the content item is lifted, the SQL statement is joined to the categories table via Category Id, so the category name can be retrieved for that given row. 

 

What I am trying to say is that you need to use a different SQL statement that refers to both your categories and content tables and uses a join on them via Category Id. 

 

Hope that helps. 

 

Rgds

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.