0

I'm having an issue comparing a value in an array with the previous value in the array and then performing the appropriate operation. Some values are identical so I want to strip them out.

What I am trying to have it do is interrogate the variable against the previous value and if they are equal display everything else except said value.

Here is my code. I apologize if its difficult to read. I'm self taught and I do my best...

<?php
// Get Variables from dept.php
$location = $_GET['location'];
$dept = $_GET['dept'];
// Connect to Database
$connect = mysql_connect('localhost', 'UserName', 'Pass');
mysql_select_db('database', $connect);
// Create Recordset       
$iso_sql = " SELECT * FROM `a_qa-iso_data`, `a_qa-iso_category` WHERE `a_qa-iso_data`.Article_Numbers = `a_qa-iso_category`.Article_No AND `a_qa-iso_data`.$location = 1 AND `a_qa-iso_data`.$dept = 1 ORDER BY `a_qa-iso_data`.Article_Numbers, `a_qa-iso_data`.Filename ASC;";
$iso_results = mysql_query($iso_sql, $connect) or die(mysql_error());
$rows_iso_results = mysql_num_rows($iso_results);
// Loop Results
while ($iso_array = mysql_fetch_array($iso_results)) {
    $article = $iso_array['Article_Numbers'];
    $title = $iso_array['Title'];
    $link = $iso_array['Link'];
    $class = $iso_array['Class'];
    $target = $iso_array['Target'];
    $filename = $iso_array['Filename'];
    $header = "<h4>$article - $title</h4>";
    $iso = "<a href=\"$link\" class=\"$class\" target=\"$target\">$filename</a>";
 // Display   
    $next = prev($iso_array);
    if ($next['Article_Numbers'] === $article) {
        echo "$iso";
    }
    else {
        echo "$header $iso";
    }
}
?>

So in short, I am looking for something like:

$article - $title
-$iso

$article - $title
-$iso
-$iso

$article - $title
-$iso
-$iso
-$iso

$article - $title
-$iso

Anyhow any help would be appreciated. I apologize as well if this has been asked before however, I've spent quite a bit of time looking and what I have found didn't seem to be as informative as I would have hoped.

3
  • It may not help answer your question, but you should stop using mysql_* functions. They're being deprecated. Instead use PDO (supported as of PHP 5.1) or mysqli (supported as of PHP 4.1). If you're not sure which one to use, read this article. Commented Aug 16, 2012 at 19:46
  • I'm aware of this however the site is going to be migrated to MSSQL server so recoding is going to be done either way, at that point I'll switch to PDO. Commented Aug 16, 2012 at 19:50
  • Have you checked for any trailing or leading spaces around the article numbers? Commented Aug 16, 2012 at 23:52

1 Answer 1

2

Why not set a temp variable for the value of "previous"?

$previous = null;
while($iso_array = mysql_fetch_array($iso_results)) {
    // code 
    .
    .
    .

    if (!is_null($previous) && $previous['Article_numbers'] == $article) {
        // something
    } else {
        // something else
    }
    $previous = $iso_array;
}

NOTE: You should stop using mysql_* functions. They're being deprecated. Instead use PDO (supported as of PHP 5.1) or mysqli (supported as of PHP 4.1). If you're not sure which one to use, read this article.

Sign up to request clarification or add additional context in comments.

3 Comments

Doesn't seem like it... I'm missing something here I'm just at a loss as to what? The pointer doesn't seem to want to skip that duplicate value...
Regardless, you're using prev() incorrectly. Try implementing my code and see if it works.
That mde all the difference!! Thanks for your help Matt!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.