Jump to content

Question about code


wwfc_barmy_army

Recommended Posts

Hi Guys.

I have this code:

[code=php:0]$result = @mysql_query("SELECT * FROM t_50 ORDER BY name ASC");
while ($row = mysql_fetch_assoc($result)) {
    if($letter != substr($row['name'],0,1)) {
print "<p/><b>";
        echo substr($row['name'],0,1);
print "</b>";
        $letter = substr($row['name'],0,1);
    }
    echo "<br/><a href=index.php?movie=".$row['id'].">".$row['name']."</a>";
}[/code]

This returns the results and sorts them into alphabeticaly order under each alphabet header prefectly but i have a couple of questions:

1. How would i go about adding 'anchor link' to each letter? Can anyone give me any advice?

2. Would it be possible to include a piece of code (advertisement) every like 4 headers? If so how would i go about this?

I'm still reasonably new to php so i have many questions  :P

Thanks!
Link to comment
Share on other sites

try this

[code]
$result = @mysql_query("SELECT * FROM t_50 ORDER BY name ASC");
  $adcount = 0;
while ($row = mysql_fetch_assoc($result)) {
    if($letter != substr($row['name'],0,1)) {
      $letter= substr($row['name'],0,1);
print "<p/><b>";  echo  "<a href=#>$letter</a>"; print "</b>";
    }
else {

if ($adcount >=4) {
    echo "<br/><a href=index.php?movie=".$row['id'].">".$row['name']."</a>";
      echo "Display ad";
    $adcount = 0;
} else {
    echo "<br/><a href=index.php?movie=".$row['id'].">".$row['name']."</a>";
  $adcount= $adcount+1;
  }
}
}
[/code]
Link to comment
Share on other sites

Hello.

Thanks for your reply! :) I can see what you have done with the ad problem but i can't quite see where the anchor links comes from, i see you put link for each letter linking to '#' but i want it so that the letters are not a link, and the links A B C D ..etc... are at the top and they are the links.

Thanks for any advice.
Link to comment
Share on other sites

ok try this

[code]
$result = @mysql_query("SELECT * FROM t_50 ORDER BY name ASC");
  $adcount = 0;
while ($row = mysql_fetch_assoc($result)) {
  $letter = strtoupper(substr($row['name'],0,1));
     
if ($letter != $header) {
      print "<p/><b><u>";  echo  "<a href=#>$letter</a>"; print "</u></b></p>";
    }

if ($adcount <4) {
    echo "<br/><a href=index.php?movie=".$row['id'].">".$row['name']."</a>";
  $adcount= $adcount+1;
} else {
    echo "<br/><a href=index.php?movie=".$row['id'].">".$row['name']."</a>";
      echo "Display ad";
    $adcount = 0;
  }

  $header = $letter;

}

[/code]
Link to comment
Share on other sites

Hi Guys.

Ok, i'm going to work on the ad code every 4 Alphabet letters. I currently have this code:
[code=php:0]$result = @mysql_query("SELECT * FROM t_50 ORDER BY name ASC");
while ($row = mysql_fetch_assoc($result)) {
    if($letter != substr($row['name'],0,1)) {
print "<p/><b>";
        echo substr($row['name'],0,1);
print "</b>";
        $letter = substr($row['name'],0,1);
  $adcount= $adcount+1;
    }
 
    echo "<br/><a href=index.php?movie=".$row['id'].">".$row['name']."</a>";

if($adcount == 4) {
      echo "Display ad";
  $adcount = 0;
  }
}[/code]

This shows the ad code every 4 alphabet letters but i can't make it so it does it at the end of all the records instead of after the first one. How can i arrange it?

Thanks.
Link to comment
Share on other sites

OK, try
[code]
<?php

$letters = range('A','Z');

foreach ($letters as $c) {
    echo "<a href='#$c'>$c</a> ";
}

$result = @mysql_query("SELECT id, name FROM t_50 ORDER BY name ASC")
                or die(mysql_error());
$lastLetter = '';
$adcount = 0;
while (list($id, $name)=mysql_fetch_row($result)) {
   
    $c = strtoupper($name{0});  // get first letter of name
    if ($c != $lastLetter) {
        if ($adcount > 0 && $adcount%4 == 0) {
            echo '<hr>Advert<hr>';
        }
        echo "<a name='$c'><h3>$c</h3></a>";
        $adcount++;
        $lastLetter = $c;
    }
    echo "<a href='index.php?movie=$id'>$name</a><br/>";
}
?>
[/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.