Jump to content

Listing MySQL table


LukeBateson

Recommended Posts

Right, it's hard to explain, but I'll try my best.

Litterally, I have a 'tab' box on my homepage.

NJHN6.png

 

And a table called 'news'. Now, the table has the rows "shorttitle" "shortstory" and "image" in it.

I want the title the three latest titles to be on the three tabs, and the story and image in the respective content areas. (The table auto increments the ID, and so order by ID descending limit of 3 yes?)

 

Now, the code for the tabs is:

<?php
$query = "SELECT * FROM news where published = '1' ORDER by id DESC LIMIT 3";
$row_news = mysql_fetch_array($query);
?>
<div id="myTabs">
         <ul>
            <li><a href="#firsttab"></a><? echo $row_news['shorttitle']; ?></li>
            <li><a href="#secondtab">newsID2</a></li>            
            <li id="last"><a href="#thirdtab">newsID3 </a></li>            
        </ul>
<div id="firsttab" class="tab_content">
<div class="rafat">
<img src="<? echo $row_news['image']; ?>" />
<p> <? echo $row_news['shortstory']; ?> </p>
</div>
</div>
<div id="secondtab" class="tab_content">
<div class="rafat">
<img src="newsIMAGE2" />
<p> newsSTORY2 </p>
</div>
</div>
<div id="thirdtab" class="tab_content">
<div class="rafat">
<img src="newsIMAGE3" />
<p> newsSTORY3 </p>
</div>
</div>
</div>

 

Now, i've filled in the first tab with the MYSQL queries, but i'm unsure how to fill in the other two, because of the way it's coded.. I can't do a 'repeating' code, because of the IDs of the divs, and the three tabs are bunched together..

 

Anyone any ideas?

Much apreciated.

 

Thanks

Luke

Link to comment
Share on other sites

Dump the results for all three records into an array, then use the array for the output

 

<?php
$query = "SELECT *
          FROM news
          WHERE published = '1'
          ORDER by id DESC
          LIMIT 3";
$row_news = mysql_fetch_array($query);
$news = array();
while($row = mysql_fetch_assoc($row_news))
{
    $news[] = $row;
}
?>
<div id="myTabs">
    <ul>
        <li><a href="#firsttab"><?php echo $news[0]['shorttitle']; ?></a></li>
        <li><a href="#secondtab"><?php echo $news[1]['shorttitle']; ?></a></li>            
        <li id="last"><a href="#thirdtab"><?php echo $news[2]['shorttitle']; ?></a></li>            
    </ul>
    <div id="firsttab" class="tab_content">
        <div class="rafat">
            <img src="<?php echo $news[0]['image']; ?>" />
            <p><?php echo $news[0]['shortstory']; ?></p>
        </div>
    </div>
    <div id="secondtab" class="tab_content">
        <div class="rafat">
            <img src="<?php echo $news[1]['image']; ?>" />
            <p><?php echo $news[1]['shortstory']; ?></p>
        </div>
    </div>
    <div id="thirdtab" class="tab_content">
        <div class="rafat">
            <img src="<?php echo $news[2]['image']; ?>" />
            <p><?php echo $news[2]['shortstory']; ?></p>
    </div>
</div>

Link to comment
Share on other sites

I got this error

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/lukebate/public_html/**/includes/functions.php on line 68

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/lukebate/public_html/**/includes/functions.php on line 70

Link to comment
Share on other sites

Change this

$row_news = mysql_fetch_array($query);
$news = array();
while($row = mysql_fetch_assoc($row_news))

 

To this

$result = mysql_query($query) or die(mysql_error());
$news = array();
while($row = mysql_fetch_assoc($result))

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • 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.