Error: Undefined Variable

Screen Shot 2015-01-02 at Fri, Jan 2, 4.57 PM.webp
Line 341:
$reviewSite = str_replace('.com', '', $URLparts[2]); // a search foreach($reviewSites) in $URLparts[2] would be more efficient

Line 344:
$reviewSiteName = $reviewSites[$reviewSite];

Code:
<?php

// Build array of selections
$getSelections=array();
if($_SERVER['QUERY_STRING']!="") {
    $params = explode('&', $_SERVER['QUERY_STRING']);
    foreach ($params as $param) {
        $name_value = explode('=', $param);
        $name = $name_value[0];
        $value = $name_value[1];
        array_push($getSelections, $name_value[1]);
    }
}

// Connect to db
include_once('connect.php');
$connection = mysqli_connect($host, $user, $password, $db) or die();
mysqli_select_db($connection, $db);

// Build list of acceptable shortnames
$acceptableShortnames = array();
$acceptableNames = array();
$shortnameCategory = array();
$acceptableShortnames_SQL = mysqli_query($connection, "SELECT DISTINCT shortname, name, category FROM compare_wearables ORDER BY name") or die(mysqli_error($connection));
while($getShortnames = mysqli_fetch_assoc($acceptableShortnames_SQL)) {
    $acceptableShortnames[] = $getShortnames['shortname'];
    $acceptableNames[$getShortnames['shortname']] = $getShortnames['name'];
    $shortnameCategory[$getShortnames['shortname']] = $getShortnames['category']; // for sidebar.php
}

// Validate selections against acceptable shortnames
$validSelections = array();
foreach($getSelections as $selection) {
    if(in_array($selection, $acceptableShortnames)) {
        $validSelections[] = $selection; 
    }
}

// If no valid selections, pick some randomly
// This would be a good place to limit selections, server-side, if desirable
$showAlert_Random = 0;
if(count($validSelections) < 1) {
    $showAlert_Random = 1;
    $randomSelections = array_rand($acceptableShortnames, 3);
    $validSelections = array();
    foreach($randomSelections as $randomSelection) {
        $validSelections[] = $acceptableShortnames[$randomSelection];
    }
}

// Get specs on valid selections
$validSelectionsStr = implode("','", $validSelections);
$specs_SQL = mysqli_query($connection, "SELECT * FROM compare_wearables WHERE shortname in('".$validSelectionsStr."') ORDER BY name") or die(mysqli_error($db));

// Build data array
$specs = array();
while($item = mysqli_fetch_assoc($specs_SQL)) {
    $fields = array_keys($item); // Can I make this more efficient?
    foreach($fields as $field) {
        $specs[$field][] = $item[$field];
    }
}

// Close connection
mysqli_close($connection);

?>

<div class="bigBanner bB2">

<?php
// Heading for side-by-side comparison
if(count($validSelections)>=2) {
    echo "<h1><i class=\"fa fa-bar-chart\"></i>&nbsp; Wearables Comparison Tool</h1>";
    ?><div><?php
    $myTitle = '';
    for($column=0; $column<count($validSelections); $column++) {
        if($column>0) {
            $myTitle .= " vs.&nbsp;";
        }
        $fullName=str_replace(" ", "&nbsp;", $specs['name'][$column]);
        $myTitle .= $fullName;
        unset($fullName);
    }
    echo $myTitle;
    $myTitle = 'Compare ' . $myTitle; // for HTML <title>
    ?></div><?php
}
// Heading for single item page
else {
    echo "<h1><i class=\"fa fa-info-circle\"></i>&nbsp; " . $specs['name'][0] . "</h1>";
    echo "<div>Features and Specifications</div>";
    $myTitle = $specs['name'][0] . ": Features and Specifications"; // for HTML <title>
}
?>

</div>

<?php

// Show alert if no selections
if($showAlert_Random == 1) {
    echo "<a href=\"/compare/\" class=\"specialButton sB3-inverse sB-xlg sB-left\">
    <strong>You did not select any wearables to compare, so here are 3 selected at random.</strong> Click here to make your own side-by-side comparison.</a>";

}
   
?>

<div class="selectOther-tinyButtons"><a href="/compare/" class="displayWhenResponsive specialButton sB1 sB-tiny"><i class="fa fa-arrow-circle-left"></i>&nbsp; Select other wearables</a></div>


<?php
// Use sticky code if comparison
if(count($validSelections)>=2) {

    echo "\t<script type=\"text/javascript\" src=\"/pages/jquery.sticky.js\"></script>
    <script>
    $(function(){
        if ($(window).width() > 700) {
            $(\".sticky\").sticky({topSpacing:0, getWidthFrom: \"#comparisonSpecs\", responsiveWidth: true});
        }
        var eventFired = 0;
        $(window).on('resize', function() {
            if (!eventFired) {
                if ($(window).width() > 700) {
                    $(\".sticky\").sticky({topSpacing:0, getWidthFrom: \"#comparisonSpecs\", responsiveWidth: true});
                }
            }
        });
    });
    </script>";

} ?>

<div id="comparison">
    <div class="sticky">
        <table class="sticky">
   
<?php

$colwidth = round(100/(count($validSelections)+1),1);

// Head row
echo "\t<tr><th class=\"specField\" width=\"" . $colwidth . "%\">&nbsp;</th>";
for($column=0; $column<count($validSelections); $column++) {
    echo "<th class=\"" . $specs['shortname'][$column] . "\" width=\"" . $colwidth . "%\"><h3>" . $specs['name'][$column] . "</h3></th>";
}
echo "</tr>\n";

// Image row
echo "\t<tr class=\"specImage\"><td class=\"specField\">&nbsp;</td>";
for($column=0; $column<count($validSelections); $column++) {
    echo "<td class=\"".$specs['shortname'][$column]." spec\"><img src=\"/images/compare-".$specs['shortname'][$column].".jpg\" alt=\"".$specs['name'][$column]."\"></td>";
}
echo "</tr>\n";

// Removal button row
if(count($validSelections)>1) {
    echo "\t<tr class=\"removeSelection\"><td class=\"specField\">&nbsp;</td>";
    for($column=0; $column<count($validSelections); $column++) {
        $newURL="/compare/specs/?";
        $subsequent=0;
        foreach($validSelections as $part) {
            if($part!=$specs['shortname'][$column]) {
                if($subsequent>=1) {
                    $newURL=$newURL."&amp;";
                }
                $newURL=$newURL."vs=".$part;
                $subsequent++;
            }
        }
        unset($subsequent);
        echo "<td class=\"".$specs['shortname'][$column]." spec specNeutral\"><a href=\"".$newURL."\" class=\"specialButton sB3-inverse sB-tiny sB-block\"><i class=\"fa fa-ban\"></i> Remove</a></td>";
        unset($removeSelectionURL);
    }
    echo "</tr>\n";
}

?>

        </table>
    </div>
    <table id="comparisonSpecs">

<?php

// Data rows
$row = 0;
$colspan = count($validSelections) + 1;
foreach($specs as $spec) {
       
    // Category rows
    $categoryBreaks = array(
        'activity_calories' => 'Activity Tracking',
        'battery_duration' => 'Battery',
        'compat_Android' => 'Compatability',
        'connect_Bluetooth' => 'Connectivity',
        'display_color' => 'Display',
        'notif_apps' => 'Notifications',
        'other_accelerometer' => 'Other Features',
        'reviews' => 'Reviews',
        'link_site' => 'Official Sites',
        'price' => 'Basic Information',
    );
    if(in_array($fields[$row], array_keys($categoryBreaks))) {
        echo "\t<tr class=\"specCategory\"><td colspan=\"" . $colspan . "\">" . $categoryBreaks[$fields[$row]] . "</td></tr>\n";
    }

    // Determine if row highlighting should be enabled
    if(count($validSelections)<=2 && count(array_unique($spec))!=1 && $fields[$row] != 'reviews' && $fields[$row] != 'link_site') {
        $highlight="specHighlight";
    } else {
        $highlight = '';
    }
   
    // Make sure row should be displayed
    $hiddenRows = array(
        'id',
        'shortname',
        'name',
        'category',
        'price_prefix',
        'price_suffix',
    );
    if(!in_array($fields[$row], $hiddenRows)) {

        // New row
        $fieldNames = array(
            'activity_calories' => 'Calories',
            'activity_distance' => 'Distance', 
            'activity_heartRate' => 'Heart rate',
            'activity_sleep' => 'Sleep',
            'activity_steps' => 'Steps',
            'activity_swim' => 'Swim',
            'activity_perspiration' => 'Perspiration',
            'battery_duration' => 'Duration',
            'battery_rechargeable' => 'Rechargeable',
            'compat_Android' => 'Android',
            'compat_iOS' => 'iOS',
            'compat_Mac' => 'Mac',
            'compat_Windows' => 'Windows',
            'connect_Bluetooth' => 'Bluetooth',
            'connect_GPS' => 'GPS',
            'connect_USB' => 'USB',
            'connect_WiFi' => 'Wi-Fi',
            'display_size' => 'Size',
            'display_resolution' => 'Resolution',
            'display_touch' => 'Touch',
            'display_type' => 'Type',
            'other_accelerometer' => 'Accelerometer',
            'other_clock' => 'Clock',
            'other_send' => 'Messaging',
            'other_voice' => 'Voice control',
            'other_waterResistant' => 'Water resistance',
            'other_wirelessSync' => 'Wireless sync',
            'notif_apps' => 'Apps',
            'notif_callerID' => 'Caller ID',
            'notif_email' => 'Email',
            'notif_event' => 'Event',
            'notif_silentAlarm' => 'Silent alarm',
            'notif_SMS' => 'SMS/text',
            'other_downloadableApps' => 'Download apps',
            'other_phoneCalls' => 'Make calls',
            'other_audio' => 'Audio',
            'connect_cell' => 'Cellular',
            'other_camera' => 'Camera',
            'other_UV' => 'UV index',
            'display_color' => 'Color',
            'price' => 'MSRP',
            'release' => 'Release',
            'link_site' => '',
            'reviews' => '',
        );
        $fieldName = $fieldNames[$fields[$row]];
        echo "\t<tr class=\"" . $fields[$row] . " " . $highlight . "\"><td class=\"specField\" width=\"" . $colwidth . "%\">" . $fieldName . "</td>";
   
        // Data cells
        for($column=0; $column<count($validSelections); $column++) {
       
            // Set cell classes and do general find/replaces
            if($spec[$column] == "Yes") {
                $class = "specYes";
                $spec[$column] = "<i class=\"fa fa-check fa-lg\"></i>";
            } elseif($spec[$column] == "No") {
                $class = "specNo";
                $spec[$column] = "<i class=\"fa fa-close fa-lg\"></i>";
            } elseif($spec[$column] == "n/a") {
                $class = "specNA";
            } else {
                $class = "specNeutral";
                if($spec[$column] == '') {
                    $spec[$column] = "&nbsp;";
                }
            }
           
            // Price row
           
            if($fields[$row] == 'price') {
                $spec[$column] = $specs['price_prefix'][$column] . "$" . $specs['price'][$column] . $specs['price_suffix'][$column];
            }
           
            // Official links row
            if($fields[$row] == 'link_site') {
                $officialSite = htmlspecialchars($spec[$column]);
                unset($spec[$column]);
                $spec[$column] = "<a class=\"specialButton sB1 sB-sm sB-block\" href=\"" . $officialSite . "\" target=\"_blank\" rel=\"nofollow\"><i class=\"fa fa-external-link\"></i>&nbsp; Official site</a>";
            }

            // Reviews row
            $reviewSites = array(
                'pcmag' => 'PC Magazine',
                'forbes' => 'Forbes',
                'cnet' => 'CNET',
                'engadget' => 'Engadget',
                'businessinsider' => 'Business Insider',
                'mashable' => 'Mashable',
                'yahoo' => 'Yahoo',
                'theverge' => 'The Verge',
                'gizmodo' => 'Gizmodo',
                'wired' => 'Wired',
                'consumerreports' => 'Consumer Reports',
                'zdnet' => 'ZDNet',
                'wsj' => 'Wall Sreet Journal',
                'blogs.wsj' => 'Wall Sreet Journal',
                'fortune' => 'Fortune',
            );
            if($fields[$row] == 'reviews') {
                $reviewURLs = explode("\n", $spec[$column]);
                $spec[$column]='';
                $reviewURLsStandard = array();
                foreach($reviewURLs as $reviewURL) {
                    $reviewURLStandard = str_replace('www.', '', $reviewURL); // a search foreach($reviewSites) in $URLparts[2] would be more efficient
                    $reviewURLsStandard[] = $reviewURLStandard;
                }
                sort($reviewURLsStandard);
                $reviewNum = 0;
                foreach($reviewURLsStandard as $reviewURL) {
                    $reviewNum++;
                    $URLparts = explode('/', $reviewURL);
                    $reviewSite = str_replace('.com', '', $URLparts[2]); // a search foreach($reviewSites) in $URLparts[2] would be more efficient
                    $reviewSite = str_replace('.org', '', $reviewSite);
                    $reviewSite = str_replace('.co.uk', '', $reviewSite);
                    $reviewSiteName = $reviewSites[$reviewSite];
                    if($reviewSiteName != '') { // There are more accurate ways to hide blank values, but they aren't working for some reason
                        $spec[$column] .= "<a class=\"specialButton sB00 sB-sm sB-block sB-left sB-marginTop review$reviewNum\" href=\"" . $reviewURL . "\" target=\"_blank\" rel=\"nofollow\"><i class=\"fa fa-external-link\"></i>&nbsp; " . $reviewSiteName . "</a>";
                    }
                }
            }
           
            // Echo the cell
            echo "<td class=\"".$specs['shortname'][$column]." spec ".$class."\" width=\"" . $colwidth . "%\"><span>".$spec[$column]."</span></td>";
        }
       
        echo "</tr>\n";
       
    }
   
    $row++;
}
?>

    </table>
</div>

<script>
$(document).ready(function(){
    $('#linkToTop').on('click', function(){
        $('html, body').animate({scrollTop: '0px'}, 800);
    }); 
});  
</script>
<br>
<br>
<div id="linkToTop" class="specialButton sB0 sB-xlg"><i class="fa fa-long-arrow-up fa-lg"></i>&nbsp; Top of page</div>
 
That suggests that $URLparts does not have more than 2 keys in the array. $reviewURL does not seem to have more than two parts to it.
Also, no need for unset($spec[$column]); on line 306 since you are already overwriting it on the next line.
 
Top Bottom