Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Skip a record when retreiving data

Status
Not open for further replies.

pheos2

Technical User
Jul 22, 2003
19
GB
I am working with Dreamweaver MX and PHP/SQL.



Is there a way to skip the first 2 rows returned from the recordset?

For Example:

If the recordset returns: 1,2,3,4,5,6,7,8,9,10. Is there a way to skip the fist 2 and start displaying rows from 3 onwards?



I want to use this on a news index page. The first two articles displayed on the page would be in a larger font and have a photo.

The rest would be display in a list of titles below that. These would be from a different recordset. Obviously I don’t want to display the articles twice so I need to skip the first two.



I understand from other posts on the net that I could use ‘substr’ but I don’t know how to go about it.



I have read the part in the PHP manual but I’m still none the wiser.

I have experimented without success. Can some kind soul explain how I would go about adding substr to the code below? I *think* you exchange it for ‘echo’?

The code on the page is:



<?php do { ?>

<a href=&quot;/view.php?id=<?php echo $row_Headlines['id']; ?>&quot;><?php echo $row_Headlines['title']; ?></a>

<?php } while ($row_Headlines = mysql_fetch_assoc($Headlines)); ?>



Many Thanks


 
I should do that like this:

for ($i=0;$i<2;$i++) //for the first two articles
{
$row = mysql_fetch_array($result);

print(&quot;<p>&quot;.$row_Headlines['title'].&quot;</p>&quot;);
print ($row_Headlines['articletext']); etc...etc...
}

while($row = mysql_fetch_array($result)) //for the links
{
print(&quot;<a href=\&quot;/view.php?id=&quot;.$row_Headlines['id'].&quot;\&quot;>&quot;.$row_Headlines['title'].&quot;</a>&quot;);
}

H.T.H.

Quasibobo

Don't eat yellow snow!
 
Thanks for you suggestion, it seems to be what I need.
I have played around trying to add this into my page but cant get it to work.

Can you help with adding this to the DW code?

I was also a bit confused by:

for ($i=0;$i<2;$i++) //for the first two articles

is the 'for' part of the code? or is the whole line a comment.

Sorry if im being stupid here, I don’t have much experience/understanding with php.

Cheers
 
what is the error? it must work fine...

Known is handfull, Unknown is worldfull
 
There is no error, Its just not working nainly because I not sure entirly what to do with the code.

As I said I don’t have much experience/understanding with php.

Thanks for your help
 
try now:

for ($i=0;$i<2;$i++)
{
$row = mysql_fetch_array($result);
print(&quot;<p>&quot;.$row_Headlines['title'].&quot;</p>&quot;);
print ($row['articletext']);
}

while($row = mysql_fetch_array($result))
{
print(&quot;<a href=\&quot;/view.php?id=&quot;.$row_Headlines['id'].&quot;\&quot;>&quot;.$row['title'].&quot;</a>&quot;);
}


Known is handfull, Unknown is worldfull
 
$result is a recordset. u have not set it in the code. before the code add this line:
$sql=&quot;select.....&quot;;
$result=mysql_query($sql);

Known is handfull, Unknown is worldfull
 
I don't use DW with PHP/SQL... Like Frontpage with regular HTML, DW creates a lot of unnesesary PHP-code. So it could get a littebit difficult to insert this code in the DW-script. It's very much possible that DW will not understand the code.
I use a text-editor like EditPlus when programming in PHP/SQL, so I know what's going on. I only use DW for the layout....

But if you post the part of the code that's handeling the SELECT-query and it's output.... maybe we can help...

Quasibobo

Don't eat yellow snow!
 
Ok, I have posted the code for two documents I’m currently playing around with.

is the page I’ve been playing around with, adding code to from this post. Change the .txt to .php to see it working. (or not as the case maybe!)

and /test2.php is what Dreamweaver produces for a recordset with repeat region. It shows what should be returned from the database.

Do worry too much though, I have decided that if I can’t get it working in the near future Ill just forget about it and go buy a vopy of the idiots guide to php instead!

Cheers for everyone’s help.
 
[tt]$row_Headlines = mysql_fetch_assoc($Headlines);[/tt] (line 6)
should be:
[tt]$row_Headlines = mysql_fetch_array($Headlines);[/tt]

[tt]print ($row['text'])[/tt] has to be [tt]print ($row_Headlines['text'])[/tt]

and the same for:
[tt]$row['title'][/tt] has to be [tt]$row_Headlines['title'][/tt]

And as far as I can see.... it should work!

H.T.H.

Quasibobo

Don't eat yellow snow!
 
Oh.... and if you want a limit to the links: (because now, if there are 1000 records in the database, this script wil generate 998 links underneath the two articles)
Changes your query:
[tt]$query_Headlines = &quot;SELECT id, title FROM bb04news ORDER BY id DESC&quot;;[/tt]
to
[tt]$query_Headlines = &quot;SELECT id, title FROM bb04news ORDER BY id DESC LIMIT 20&quot;;[/tt]

(for 18 links + 2 articles.... change it in whatever you want...)

Quasibobo



Don't eat yellow snow!
 
Still not working I’m afraid :(

I made the changes you suggested and also noticed that the recordset wasn't calling the 'text' column, so I added that in.

The errors are all Occurring on a row where
$row = mysql_fetch_array($result);
is present

The changed code and page are at the address above.

Thanks for your help
 
Code:
<?php
mysql_select_db($database_PheosMain, $PheosMain);
$query_Headlines = &quot;SELECT id, title, text FROM bb04news ORDER BY id DESC LIMIT 20&quot;;
$Headlines = mysql_query($query_Headlines, $PheosMain) or die(mysql_error());
$row_Headlines = mysql_fetch_array($Headlines);
//$totalRows_Headlines = mysql_num_rows($Headlines); why as we know the total is 22 based on the limit
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
</head>

<body><?php for ($i=0;$i<20;$i++)
{
   $row = mysql_fetch_array($result);
   
   if ($i<2){   //first two records 
      //feel free to add other style elements or link to a
      //css stylesheet
      print(&quot;<h2><p>&quot;.$row_Headlines['title'].&quot;</p></h2>&quot;);
      print (&quot;<b>&quot;.$row_Headlines['text'].&quot;</b>&quot;);
   }else{
     if ($i==2){print &quot;<hr>&quot;;} 
     
     //change to another style here

     print(&quot;<a href=\&quot;/view.php?id=&quot;.$row_Headlines['id'] .&quot;\&quot;>&quot;.$row_Headlines['title'].&quot;</a>&quot;);
}
 ?>
</body>
</html>
<?php
mysql_free_result($Headlines);
?>

hth


Bastien

cat, the other other white meat
 
My mistake:

This should be the code:

[tt]<?php require_once('../Connections/PheosMain.php');

mysql_select_db($database_PheosMain, $PheosMain);
$query_Headlines = &quot;SELECT id, title, text FROM bb04news ORDER BY id DESC LIMIT 20&quot;;
$Headlines = mysql_query($query_Headlines, $PheosMain) or die(mysql_error());

?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
</head>

<body>
<?php
for ($i=0;$i<2;$i++)
{
$row_Headlines = mysql_fetch_array($Headlines); print(&quot;<p>&quot;.$row_Headlines['title'].&quot;</p>&quot;);
print ($row_Headlines['text']);
}

print (&quot;<hr>&quot;); // or something else like Bastian suggested

while($row_Headlines = mysql_fetch_array($Headlines))
{
print(&quot;<a href=\&quot;/view.php?id=&quot;.$row_Headlines['id'].&quot;\&quot;>&quot;.$row_Headlines['title'].&quot;</a>&quot;);
}

mysql_free_result($Headlines);
?>
</body>
</html>[/tt]


And to Bastian: I agree on the removing of the '$totalRows_Headlines '. But for the rest, I do not see why your script has the advantage over mine.... If you want to change the limit now, you'll have to change it on two places (and possibly forgetting one). My code only relies on the LIMIT in the query...
I'd prefer this if using your code:

[tt]$limit=20; //declare limit once...
$query_Headlines = &quot;SELECT id, title, text FROM bb04news ORDER BY id DESC LIMIT $limit&quot;;[/tt]

and in the loop:
[tt]for ($i=0;$<$limit;$i++)[/tt]

Don't eat yellow snow!
 
Quasi: true about handling the limit that way...my intention was only to show that the loop can be handled another way...

here is the corrected code for my example
Code:
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
</head>

<body>
<?php
mysql_select_db($database_PheosMain, $PheosMain);

$query_Headlines = &quot;SELECT id, title, text FROM bb04news ORDER BY id DESC LIMIT 20&quot;;
$Headlines = mysql_query($query_Headlines, $conn) or die(mysql_error());
//$row_Headlines = mysql_fetch_array($Headlines);
//$totalRows_Headlines = mysql_num_rows($Headlines); why as we know the total is 22 based on the limit

$i=0;
while ($row_Headlines = mysql_fetch_array($Headlines)){
   if ($i<2){   //first two records
      //feel free to add other style elements or link to a
      //css stylesheet
      print(&quot;<h2><p>&quot;.$row_Headlines['title'].&quot;</p></h2>&quot;);
      print (&quot;<b>&quot;.$row_Headlines['text'].&quot;</b>&quot;);
      $i++;
   }else{
     if ($i==2){
       print &quot;<hr>&quot;;
     }

     //change to another style here

     print(&quot;<a href=\&quot;/view.php?id=&quot;.$row_Headlines['id'].&quot;\&quot;>&quot;.$row_Headlines['title'].&quot;</a><br>&quot;);
     $i++;
   }
}
 ?>
</body>
</html>

Bastien

cat, the other other white meat
 
Thanks Quasibobo, All working perfectly now!

Thanks for your patience!!
Cheers
Tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top