-4

I need to be able to put part of a site i had built previously into another site im making now. However, both are in php and there are issues with the syntax (ie too many subsets of " and '). How can I put this code:

        if(mysql_num_rows($result) == 0)
           {
    echo 'No categories defined yet.';
         }
          else
        {
    //prepare the table
    echo '<table border="1">
          <tr>
            <th>Category</th>
            <th>Last topic</th>
          </tr>';   

    while($row = mysql_fetch_assoc($result))
    {               
        echo '<tr>';
            echo '<td class="leftpart">';
                echo '<h3><a href="category.php?id=' . $row['cat_id'] . '">' . $row['cat_name'] . '</a></h3>' . $row['cat_description'];
            echo '</td>';
            echo '<td class="rightpart">';

            //fetch last topic for each cat
                $topicsql = "SELECT
                                topic_id,
                                topic_subject,
                                topic_date,
                                topic_cat
                            FROM
                                topics
                            WHERE
                                topic_cat = " . $row['cat_id'] . "
                            ORDER BY
                                topic_date
                            DESC
                            LIMIT
                                1";

                $topicsresult = mysql_query($topicsql);

                if(!$topicsresult)
                {
                    echo 'Last topic could not be displayed.';
                }
                else
                {
                    if(mysql_num_rows($topicsresult) == 0)
                    {
                        echo 'no topics';
                    }
                    else
                    {
                        while($topicrow = mysql_fetch_assoc($topicsresult))
                        echo '<a href="topic.php?id=' . $topicrow['topic_id'] . '">' . $topicrow['topic_subject'] . '</a> at ' . date('d-m-Y', strtotime($topicrow['topic_date']));
                    }
                }
            echo '</td>';
        echo '</tr>';
    }
}

into this code (i want the table itself to be inside the div main and under the welcome)

    echo

      "<div id=holder>
      <div id='nav'>
      <a href='chat.php'>Chat with your friends!</a> 
      <br><br><a href='changepassword.php'>Change password</a><br><br>
      <a href='logout.php'>Logout</a> 
      </div>
      <div id='main'>
      Welcome, ".$_SESSION['user_name']."!





      </div> 
      </div>";

            //fetch last topic for each cat
                $topicsql = "SELECT
                                topic_id,
                                topic_subject,
                                topic_date,
                                topic_cat
                            FROM
                                topics
                            WHERE
                                topic_cat = " .           $row['cat_id'] . "
                            ORDER BY
                                topic_date
                            DESC
                            LIMIT
                                1";

                $topicsresult = mysql_query($topicsql);

                if(!$topicsresult)
                {
                    echo 'Last topic could not be displayed.';
                }
                else
                {
                    if(mysql_num_rows($topicsresult) == 0)
                    {
                        echo 'no topics';
                    }
                    else
                    {
                        while($topicrow =           mysql_fetch_assoc($topicsresult))
                        echo '<a href="topic.php?id=' .                     $topicrow['topic_id'] . '">' . $topicrow['topic_subject'] . '</a> at ' . date('d-m-Y',           strtotime($topicrow['topic_date']));
                    }
                }
            echo '</td>';
        echo '</tr>';
    }
   }
      };

I recognize that this is pretty complicated, but I am so lost in the syntax at this point, so if someone could help me a little bit on this itd be very appreciated

ryno
  • 304
  • 5
  • 14

2 Answers2

1

Assuming that you can have both files present on the same location you can include the file with the table inside the file with the "welcome" message:

echo "
<div id=holder>
  <div id='nav'>
    <a href='chat.php'>Chat with your friends!</a> 
    <br><br>
    <a href='changepassword.php'>Change password</a>
    <br><br>
    <a href='logout.php'>Logout</a> 
  </div>
  <div id='main'>
    Welcome, ".$_SESSION['user_name']."!";

    include("path_to_the_table_file.php");

  echo "
  </div> 
</div>";

...

Link to documentation: The include statement includes and evaluates the specified file.

Zuul
  • 16,217
  • 6
  • 61
  • 88
1

Did you know? You don't have to mess with complicated echo syntax. This will work too:

<div id=holder>
<div id='nav'>
<a href='chat.php'>Chat with your friends!</a> 
<br><br><a href='changepassword.php'>Change password</a><br><br>
<a href='logout.php'>Logout</a> 
</div>
<div id='main'>
Welcome, <?php echo $_SESSION['user_name'] ?>!

<?php include("yourotherfile.php"); ?>

</div> 
</div>

<?php

        //fetch last topic for each cat
            $topicsql = "SELECT
                            topic_id,
                            topic_subject,
                            topic_date,
                            topic_cat
                        FROM
                            topics
                        WHERE
                            topic_cat = " .           $row['cat_id'] . "
                        ORDER BY
                            topic_date
                        DESC
                        LIMIT
                            1";

            $topicsresult = mysql_query($topicsql);

            if(!$topicsresult)
            {
                echo 'Last topic could not be displayed.';
            }
            else
            {
                if(mysql_num_rows($topicsresult) == 0)
                {
                    echo 'no topics';
                }
                else
                {
                    while($topicrow =           mysql_fetch_assoc($topicsresult))
                    echo '<a href="topic.php?id=' .                     $topicrow['topic_id'] . '">' . $topicrow['topic_subject'] . '</a> at ' . date('d-m-Y',           strtotime($topicrow['topic_date']));
                }
            }
        echo '</td>';
    echo '</tr>';
}
}
  };

?>
Oliver
  • 2,864
  • 1
  • 16
  • 27