Help with Search Script

Started on the old forum in March 2013 · view the original in the archive · old links redirect here

6 replies · 719 views · started 13 years ago · 1 min read

4 participants
brentg Developer · 95 posts #1

Hi

Im getting this error code on my index

Warning: Invalid argument supplied for foreach() in /home//public_html/12/html.tpl.php on line 3

this is the code in html.tpl.php

<?php include 'head.inc.php'; ?>
         <div class="rmenu"><img src="a.gif" /> Search Results: [<?=$q?>]</div> 
      <?php foreach ($data['results'] as $result) :?>
         <div class="rmenu"><a href="<?php printf('tweak.php?u=%s&h=%d&w=%d', urlencode($result['url']), $result['height'], $result['width']); ?>"><img src="<?=$result['tbUrl'] ?>" alt="<?=$result['titleNoFormatting'] ?>" width="116" height="78"/></a><div class="text"><?=$result['titleNoFormatting'] ?></div></div>
      <?php endforeach ?>
      <div id="rmenu">
         <center><a href="<?php printf('?q=%s&safe=%s&size=%s&type=%s&p=%d', $GS -> search, $GS -> safe, $GS -> image_size, $GS -> image_type, ($p - 1))?>">Prev</a> | <a href="<?php printf('?q=%s&safe=%s&size=%s&type=%s&p=%d', $GS -> search, $GS -> safe, $GS -> image_size, $GS -> image_type, ($p + 1)) ?>">Next</a></center></div>

this is what is included in valid.php which i think is causing error because when i put something in $s = array(''); like $s = array('SOMETHING HERE'); then the error goes away

<?php
$safe = array("moderate", "active");
$image_type = array('', 'jpg', 'png', 'gif', 'bmp');
$image_size = array('',);
$s = array('SOMETHING HERE');
shuffle($s);

i tried messing with code but not getting anywhere

Free Mobile Downloads: http://playwap.mobi

s3nzo Developer · 156 posts #2

brentg wrote: this is what is included in valid.php

I'm not sure if valid.php is what you should be showing us, where is

$data = array('values');

? Maybe you should be posting

head.inc.php

instead

CreativityKills Lead · 833 posts #3

Your error is a simple one. The php function foreach is not getting a valid argument. Usually this is because the first argument passed to it isn't an array. You can debug by dumping the value of $data['results'] and seeing if its actually an array.

A good dumping function I always use is this:

function dd($var)
{
    ?><pre><?php print_r($var) ?></pre><?php
    die;
}

Then you can run $data right through the function dd($data) to see if there's an array with a key "result" and if that keys value is indeed an array itself.

brentg Developer · 95 posts #4

Thanks so much all.

@Creativekills might this be helpful?

<?php include 'head.inc.php'; ?>
         <div class="rmenu"><img src="a.gif" /> Search Results: [<?=$q?>]</div> 
      <?php foreach ($data['results'] as $result) :?>
         <div class="rmenu"><a href="<?php printf('tweak.php?u=%s&h=%d&w=%d', urlencode($result['url']), $result['height'], $result['width']); ?>"><img src="<?=$result['tbUrl'] ?>" alt="<?=$result['titleNoFormatting'] ?>" width="116" height="78"/></a><div class="text"><?=$result['titleNoFormatting'] ?></div></div>
      <?php endforeach ?>
      <div id="rmenu">
         <center><a href="<?php printf('?q=%s&safe=%s&size=%s&type=%s&p=%d', $GS -> search, $GS -> safe, $GS -> image_size, $GS -> image_type, ($p - 1))?>">Prev</a> | <a href="<?php printf('?q=%s&safe=%s&size=%s&type=%s&p=%d', $GS -> search, $GS -> safe, $GS -> image_size, $GS -> image_type, ($p + 1)) ?>">Next</a></center></div>
<?php
echo "<div class=\"rmenu\"><center>";
echo '<div class="header">';
echo '<font color="black">Copyright © /font><br>';
echo '<a href="/disclaimer.php">Disclaimer</a><br/>';
echo "</div>";
echo "</body></html>";
?>

Free Mobile Downloads: http://playwap.mobi

arnage Lead · 811 posts #6

$data['results'] probably isn't an array, post from what you assigned that var.


The error is showing when the search is empty.

<?php include 'head.inc.php'; ?> 
         <div class="rmenu"><img src="a.gif" /> Search Results: [<?=$q?>]</div>  
      <?php
$results = !empty($data['results']) ? $data['results'] : null;
if (isset($results)) {
 foreach ($results as $result) {?> 
         <div class="rmenu"><a href="<?php printf('tweak.php?u=%s&h=%d&w=%d', urlencode($result['url']), $result['height'], $result['width']); ?>"><img src="<?=$result['tbUrl'] ?>" alt="<?=$result['titleNoFormatting'] ?>" width="116" height="78"/></a><div class="text"><?=$result['titleNoFormatting'] ?></div></div> 
      <?php } }?> 
      <div id="rmenu"> 
         <center><a href="<?php printf('?q=%s&safe=%s&size=%s&type=%s&p=%d', $GS -> search, $GS -> safe, $GS -> image_size, $GS -> image_type, ($p - 1))?>">Prev</a> | <a href="<?php printf('?q=%s&safe=%s&size=%s&type=%s&p=%d', $GS -> search, $GS -> safe, $GS -> image_size, $GS -> image_type, ($p + 1)) ?>">Next</a></center></div>
<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***
brentg Developer · 95 posts #7

This the code in valid.php.

 <?php
$safe = array("moderate", "active");
$image_type = array('', 'jpg', 'png', 'gif', 'bmp');
$image_size = array('',);
$s = array('SOMETHING HERE');
shuffle($s); 

when you insert a word in-place of "SOMETHING HERE" then the error goes away.

Free Mobile Downloads: http://playwap.mobi

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close