Jump to content

[SOLVED] PHP Table with Multiple Rows - Help


hank9481

Recommended Posts

I have a MySQL table with 7 fields. An ID field distinguishes each record because multiple records of data are stored in the table. What I want to do is create a PHP script that will display the most recent 5 records of data.

 

Can someone give me some idea of what I have done wrong? Admittedly, I once knew how to do this, but having not dabbled with PHP for some years now, I have forgotten and am very much a novice.

 

Right now, I only get the most recent entry. The key bits of the script are below:

 

//SELECT UPDATEABLE FIELDS

$result = mysql_query("SELECT date, pic, title, name, team, text, pic FROM rumor LIMIT 5") or die(mysql_error());

while ($row = @mysql_fetch_array($result,MYSQL_ASSOC)) {

 

//HEADLINES

$date=$row{'date'};

$pic="http://files.mblsim.com/reports/news/html/images/".$row{'pic'}."";

$title=$row{'title'};

$name=$row{'name'};

$team=$row{'team'};

$text=$row{'text'};

 

<table width="100%" height="100%" border="0" cellpadding="5" cellspacing="0">

                            <tr>

                              <td valign="top"><table width="100%" border="0" cellspacing="1" cellpadding="1">

                                  <tr>

                                    <td class="rumorHeader">MBL Rumor Central:

                                      <?php print $title; ?></td>

                                  </tr>

                                  <tr>

                                    <td bgcolor="#000066" class="dateBorder"><font color="#FFFFFF">Updated

                                      <?php print $date; ?> </font></td>

                                  </tr>

                                  <tr>

                                    <?php print "<td class=rumorPad> <table width=100% border=0 cellspacing=1 cellpadding=1>

                                        <tr>

                                          <td width=105 rowspan=3 valign=top>

                                            <div align=left><img src=".$pic." width=90 height=135 border=0></div></td>

                                          <td class=rumorTitle>".$title."</td>

                                        </tr>

                                        <tr>

                                          <td class=rumorName>".$name." | ".$team."</td>

                                        </tr>

                                        <tr>

                                          <td valign=top class=rumorText>

                                            <p>".$text."</p></td>

                                        </tr>

                                      </table></td>" ?>

                                  </tr>

                                </table></td>

                            </tr>

                          </table>

Link to comment
Share on other sites

I think you could try [] brackets instead of {} brackets around your php values.

also, I think you want to do it more like this:

<table>

<?php while (something) { ?>

<tr>

  <td></td>

</tr>

<?php } //end something ?>

</table>

 

 

Ha, you confused me immeasurably.

Link to comment
Share on other sites

Your problem is with your logic, you are declaring the variables which are not an array multiple times in a while loop.

 

You have two options.

 

1) You can output the variables in the while loop; for example

 

while ($row = @mysql_fetch_array($result,MYSQL_ASSOC)) {

 

//HEADLINES

$date=$row{'date'};

$pic="http://files.mblsim.com/reports/news/html/images/".$row{'pic'}."";

$title=$row{'title'};

$name=$row{'name'};

$team=$row{'team'};

$text=$row{'text'};

 

echo "date is $date<br>";

echo "pic is $pic<br>";

 

}

 

This will print the date and pic for each item throughout the while loop.

 

2) You could store the items in an array and reference them later outside of the while loop

 

//HEADLINES

$date[]=$row{'date'};

$pic[]="http://files.mblsim.com/reports/news/html/images/".$row{'pic'}."";

$title[]=$row{'title'};

$name[]=$row{'name'};

$team[]=$row{'team'};

$text[]=$row{'text'};

 

then later in your code you could reference date[0], date[1], etc etc.

Link to comment
Share on other sites

or you could use fetch_assoc instead of array.

<?php
//SELECT UPDATEABLE FIELDS
$result = mysql_query("SELECT date, pic, title, name, team, text, pic FROM rumor LIMIT 5") or die(mysql_error());
while ($row = mysql_fetch_assoc($result) {

//HEADLINES
$date=$row['date'];
$pic="http://files.mblsim.com/reports/news/html/images/".$row['pic']."";
$title=$row['title'];
$name=$row['name'];
$team=$row['team'];
$text=$row['text'];
?>
<table width="100%" height="100%" border="0" cellpadding="5" cellspacing="0">
                            <tr>
                              <td valign="top"><table width="100%" border="0" cellspacing="1" cellpadding="1">
                                  <tr>
                                    <td class="rumorHeader">MBL Rumor Central:
                                      <?php print $title; ?></td>
                                  </tr>
                                  <tr>
                                    <td bgcolor="#000066" class="dateBorder"><font color="#FFFFFF">Updated
                                      <?php print $date; ?> </font></td>
                                  </tr>
                                  <tr>
                                    <?php print "<td class=rumorPad> <table width=100% border=0 cellspacing=1 cellpadding=1>
                                        <tr>
                                          <td width=105 rowspan=3 valign=top>
                                            <div align=left><img src=".$pic." width=90 height=135 border=0></div></td>
                                          <td class=rumorTitle>".$title."</td>
                                        </tr>
                                        <tr>
                                          <td class=rumorName>".$name." | ".$team."</td>
                                        </tr>
                                        <tr>
                                          <td valign=top class=rumorText>
                                            <p>".$text."</p></td>
                                        </tr>
                                      </table></td>" ?>
                                  </tr>
                                </table></td>
                            </tr>
                          </table>

<?php } ?>

Link to comment
Share on other sites

Oddly enough, I changed it to assoc and it still doesn't work. Apparently, I am completely missing something small, but being horrible at this, I am hoping someone can spot what the heck I am doing wrong. Here is the entire code with the PHP related data in bold, maybe this will help?

 

//SELECT UPDATEABLE FIELDS

$result = mysql_query("SELECT date, pic, title, name, team, text FROM rumor LIMIT 5") or die(mysql_error());

while ($row = mysql_fetch_assoc($result)) {

 

//HEADLINES

$date=$row['date'];

$pic="http://files.mblsim.com/reports/news/html/images/".$row['pic']."";

$title=$row['title'];

$name=$row['name'];

$team=$row['team'];

$text=$row['text'];

 

//CLOSE OUT WebUpdater Code

?>

 

<html>

 

<head>

<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

<title>MBLsim.com :: Rumor Central</title>

<meta name="author" content="Jay Therrien, jay@mblsim.com">

<meta http-equiv="Cache-Control" content="no-cache">

<link href="../scripts/mbl.css" rel="stylesheet" type="text/css">

<script language="JavaScript" SRC="../scripts/tabs.js"></script>

<link rel="stylesheet" type="text/css" href="../scripts/contentslider.css" />

<script type="text/javascript" src="../scripts/contentslider.js">

/***********************************************

* Featured Content Slider- © Dynamic Drive DHTML code library (www.dynamicdrive.com)

* This notice MUST stay intact for legal use

* Visit Dynamic Drive at http://www.dynamicdrive.com/ for this script and 100s more

***********************************************/

</script>

<script language="JavaScript" type="text/JavaScript">

<!--

function MM_preloadImages() { //v3.0

  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();

    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)

    if (a.indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a;}}

}

 

function MM_swapImgRestore() { //v3.0

  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a)&&x.oSrc;i++) x.src=x.oSrc;

}

 

function MM_findObj(n, d) { //v4.01

  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {

    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}

  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[n];

  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers.document);

  if(!x && d.getElementById) x=d.getElementById(n); return x;

}

 

function MM_swapImage() { //v3.0

  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)

  if ((x=MM_findObj(a))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}

}

//-->

</script>

<script language="JavaScript" type="text/JavaScript">

<!--

function MM_reloadPage(init) {  //reloads the window if Nav4 resized

  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {

    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}

  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();

}

MM_reloadPage(true);

//-->

</script>

<script>

<!--

 

function showhide(id){

if (document.getElementById){

obj = document.getElementById(id);

if (obj.style.display == "none"){

obj.style.display = "";

} else {

obj.style.display = "none";

}

}

}

 

function MM_swapImgRestore() { //v3.0

  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a)&&x.oSrc;i++) x.src=x.oSrc;

}

 

function MM_preloadImages() { //v3.0

  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();

    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)

    if (a.indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a;}}

}

 

function MM_findObj(n, d) { //v4.01

  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {

    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}

  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[n];

  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers.document);

  if(!x && d.getElementById) x=d.getElementById(n); return x;

}

 

function MM_swapImage() { //v3.0

  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)

  if ((x=MM_findObj(a))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}

}

//-->

</script>

 

</head>

 

<body bgcolor="#333333" onLoad="MM_preloadImages('../New%20Site/Images/navbar/ootpou_over.gif','../New%20Site/Images/navbar/constitution_over.gif','../New%20Site/Images/navbar/gm_over.gif','../New%20Site/Images/navbar/forum_over.gif','../New%20Site/Images/navbar/file_over.gif','../New%20Site/Images/navbar/calendar_over.gif','../New%20Site/Images/navbar/blog_over.gif','../images/navbar/historyro.gif','../images/navbar/directoryro.gif','../images/navbar/constitutionro.gif','../images/navbar/calendarro.gif','../images/navbar/blogro.gif','../images/navbar/forumro.gif','../images/navbar/ootpouro.gif','../images/navbar/rumors_over.gif')">

<div align="center">

  <table width="980" border="0" align="center" cellpadding="0" cellspacing="0">

    <tr>

      <td colspan="3"><div align="center"><a href="http://www.mblsim.com/home.php"><img src="../images/header.gif" width="980" height="90" border="0"></a></div></td>

    </tr>

    <tr>

      <td bgcolor="#990000"> </td>

      <td width="150" bgcolor="#990000"> <div align="left"></div>

        <div align="left"><img src="../images/navbar/ootp.gif" width="150" height="18"></div></td>

      <td bgcolor="#990000"> <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">

          <tr>

            <td><div align="center"><a href="history.php?year=2017" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('History','','../images/navbar/historyro.gif',1)"><img src="../images/navbar/history.gif" name="History" width="100" height="18" border="0"></a></div></td>

            <td><div align="center"><a href="gm_directory.php" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Directory','','../images/navbar/directoryro.gif',1)"><img src="../images/navbar/directory.gif" name="Directory" width="100" height="18" border="0"></a></div></td>

            <td><div align="center"><a href="rumors.php" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Rumors','','../images/navbar/rumors_over.gif',1)"><img src="../images/navbar/rumors.gif" name="Rumors" width="100" height="18" border="0"></a></div></td>

            <td><div align="center"><a href="constitution.php" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Constitution','','../images/navbar/constitutionro.gif',1)"><img src="../images/navbar/constitution.gif" name="Constitution" width="100" height="18" border="0"></a></div></td>

            <td><div align="center"><a href="calendar.php" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Calendar','','../images/navbar/calendarro.gif',1)"><img src="../images/navbar/calendar.gif" name="Calendar" width="100" height="18" border="0"></a></div></td>

            <td><div align="center"><a href="mbl_blog/" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Blog','','../images/navbar/blogro.gif',1)"><img src="../images/navbar/blog.gif" name="Blog" width="100" height="18" border="0"></a></div></td>

            <td><div align="center"><a href="mbl_board/" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Forum','','../images/navbar/forumro.gif',1)"><img src="../images/navbar/forum.gif" name="Forum" width="100" height="18" border="0"></a></div></td>

            <td><div align="center"><a href="http://files.mblsim.com/ootpsqlou/exports.php" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image114','','../images/navbar/ootpouro.gif',1)"><img src="../images/navbar/ootpou.gif" name="Image114" width="100" height="18" border="0"></a></div></td>

          </tr>

        </table></td>

    </tr>

                      <td valign="top">

                        <div id="Layer1" style="position:relative; width:770; height:446; z-index:1; left: 0; top: 0; overflow: scroll; background-color: #FFFFFF; layer-background-color: #FFFFFF; border: 1px none #000000;">

                          <table width="100%" height="100%" border="0" cellpadding="5" cellspacing="0">

                            <tr>

                              <td valign="top"><table width="100%" border="0" cellspacing="1" cellpadding="1">

                                  <tr>

                                    <td class="rumorHeader">MBL Rumor Central:

                                      <?php print $title; ?></td>

                                  </tr>

                                  <tr>

                                    <td bgcolor="#000066" class="dateBorder"><font color="#FFFFFF">Updated

                                      <?php print $date; ?> </font></td>

                                  </tr>

                                  <tr>

                                    <?php print "<td class=rumorPad> <table width=100% border=0 cellspacing=1 cellpadding=1>

                                        <tr>

                                          <td width=105 rowspan=3 valign=top>

                                            <div align=left><img src=".$pic." width=90 height=135 border=0></div></td>

                                          <td class=rumorTitle>".$title."</td>

                                        </tr>

                                        <tr>

                                          <td class=rumorName>".$name." | ".$team."</td>

                                        </tr>

                                        <tr>

                                          <td valign=top class=rumorText>

                                            <p>".$text."</p></td>

                                        </tr>

                                      </table></td>" ?>

 

Link to comment
Share on other sites

what does this give you in your browser?

what if you just put this in the page:

<?php
//SELECT UPDATEABLE FIELDS
$result = mysql_query("SELECT date, pic, title, name, team, text FROM rumor LIMIT 5") or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {

//HEADLINES
$date=$row['date'];
$pic="http://files.mblsim.com/reports/news/html/images/".$row['pic']."";
$title=$row['title'];
$name=$row['name'];
$team=$row['team'];
$text=$row['text'];

echo $text;

}
//CLOSE OUT WebUpdater Code
?>

I don't see where you are setting a connection or selecting a database so I'm guessing it won't work. But let us know.

Separately, after you get that figured out, when doing this:

while x {

do something

}

 

what you are telling php to do is to repeat an action as it loops through the rows in your sql output. for each row in your output it will (or at least try) to do the action you tell it to do.

 

also, wrap your code in code (button that looks like the pound sign) tags not quote tags

 

[/code]

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.