Jump to content

ForEach Range Problem


CodeMama

Recommended Posts

:facewall: I have this script that works, just a couple tweeks needed...

1. I don't want it to default to A being selected

2. I need the number range part to only display one # sign not 10 of them, somehow I have to have it pull any record that does not start with alpha

 

   <?php
                                                          $letter = isset($_GET['letter']) ? $_GET['letter'] :"A";
                                                        
                                                        echo '<div align="center"><b>';
                                                        foreach(range('A','Z') as $c){
                                                          ($letter == $c)
                                                            ? printf('%s&nbsp',$c)
                                                            : printf('<a href="browse.php?letter=%s">%s</a> ',$c,$c);
                                                        }
                                                    
                                                        echo '<br>';
                                                        //Other
                                                        
                                                         foreach(range('0','9') as $n){
                                                          ($letter == $n)
                                                        ? printf('%s&nbsp',$n)
                                                        : printf('<a href="?letter=%s">#</a> ',$n,$n);
                                                      }
                                                    
                                                        echo "</b><br></div><p>";

                              ?> 

Link to comment
Share on other sites

1. I don't want it to default to A being selected

This line

$letter = isset($_GET['letter']) ? $_GET['letter'] :"A";

Will assign the letter A to the $letter variable by default if the variable $_GET['letter'] doesn't exist. Use the following if you don't want $letter to have a default value

$letter = isset($_GET['letter']) ? $_GET['letter'] : null;

 

2. I need the number range part to only display one # sign not 10 of them, somehow I have to have it pull any record that does not start with alpha

This

($letter == $n)
                                                        ? printf('%s&nbsp',$n)
                                                        : printf('<a href="?letter=%s">#</a> ',$n,$n);

Needs to be

($letter == $n)
                                                        ? printf('%s&nbsp',$n)
                                                        : printf('<a href="?letter=%s">%s</a> ',$n,$n);

 

Now numbers will be displayed rather than #'s

 

 

 

Link to comment
Share on other sites

Thanks, but I have the numbers displayed and boss wants just 1 # sign displayed and then the view page to list all records that don't start with alpha since there are really only a few of them that begin with a number...

 

Link to comment
Share on other sites

I suggest separating your logic from the actual display of the content. Makes it much easier in the log run. In any event, this should get you what you want. You can put the logic (the top part) in a separate page or at the top of your code. Then put the last line within the context of your page.

 

<?php
    //Create array with letters AND number sign
    $letters = range('A','Z');
    array_push($letters, '#');

    $menu = '';
    foreach($letters as $letter) {
        $menu .= ($letter == $_GET['letter'])
              ? sprintf('%s&nbsp', $letter)
              : sprintf('<a href="browse.php?letter=%s">%s</a> ', $letter, $letter);
    }

    echo "<div align=\"center\"><b>{$menu}</b><br /></div>";
?>

Link to comment
Share on other sites

Ah,that was me being lazy. You need to define the "default" like you had before with wildteen's update. My code was referencing $_GET['letter'] without verifying it was set.

 

<?php

error_reporting(E_ALL);

    //Create array with letters AND number sign
    $letters = range('A','Z');
    array_push($letters, '#');

    $menu = '';
    $selectedLetter = isset($_GET['letter']) ? $_GET['letter'] : null;
    foreach($letters as $letter) {
        $menu .= ($letter == $selectedLetter)
              ? sprintf('%s&nbsp', $letter)
              : sprintf('<a href="browse.php?letter=%s">%s</a> ', $letter, $letter);
    }

    echo "<div align=\"center\"><b>{$menu}</b><br /></div>";
?>

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.