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
https://forums.phpfreaks.com/topic/28257-question-about-code/
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
https://forums.phpfreaks.com/topic/28257-question-about-code/#findComment-129278
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
https://forums.phpfreaks.com/topic/28257-question-about-code/#findComment-129303
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
https://forums.phpfreaks.com/topic/28257-question-about-code/#findComment-129310
Share on other sites

Here's an example

[code]
<?php
$letters = range('A','Z');

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

foreach ($letters as $c) {
    echo "<a name='$c'><h3>$c</h3></a>";
    for ($i=0; $i<5; $i++) {
        echo "<p>$i</p>";
    }
}
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/28257-question-about-code/#findComment-129315
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
https://forums.phpfreaks.com/topic/28257-question-about-code/#findComment-129628
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
https://forums.phpfreaks.com/topic/28257-question-about-code/#findComment-129718
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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