Forum Moderators: coopster

Message Too Old, No Replies

In this PHP code - something is not ok

         

zeus

12:21 am on Dec 16, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Now first I dont have a clue about PHP, but I have noticed that when i make a link check I get 2-4 broken links to related images pages which are ex. domain.com/widget/blue-widget/house and this house item dont fit there, means the sub categories are not correct to show house.

Now on site it do show the correct links, with correct sub categories in url to related images, when you view the site and look at source of code in browser, but as said when i make a link check with a tool, it shows those fake links also.

I did have one to fix this problem so it showed correct on site which it also do now, but maybe the "bad code is still on the coding"

here is the coding "i think"

<?php
$img1=$csv->usersSearch("wallpaper_tbl_category",array( 'category_id' => 3),'*','');
$img2=$csv->usersSearchOrder("wallpaper_tbl_sub_category",array( 'category_id' => $img1[0][category_id]),'*','4','rand()');

/*echo "<br />Category name - ".$img1[0][category_name]."<br />
subcategory name - ".$img2[0][sub_category_name]."<br />";*/
if(empty($img2[0][sub_category_id])):
$random_id=rand(32,34);
$img2_id=$random_id;;
else:
$img2_id=$img2[0][sub_category_id];
endif;
$img3=$csv->usersSearchOrder("wallpaper_tbl_sub_sub_category",array( 'sub_category_id' => $img2_id),'*','','rand()');

/*if(empty($img3[0][sub_sub_category_id])):
$random_id=rand(5,8)
$img3_id=$random_id;
else:
$img3_id=$img2[0][sub_sub_category_id];
endif;*/
/*echo "<br />subsubcategory_name - ".$img3[0][sub_sub_category_name];*/
$sub=$csv->usersSearchOrder("wallpaper_tbl_item",array('sub_sub_category_id' => $img3[0][sub_sub_category_id]),'*','4','rand()');
/*echo "<br />item name - ".$sub[0][item_name]."<br />";*/
?>
<div style="padding:8px 60px;" align="center">

<div>
<?php
/*if(count($sub)==4):*/
// var_dump($sub);
for($k=0;$k<count($sub);$k++)
{
$query = "
SELECT wi.item_name, wc.category_name, wsc.sub_category_name, wssc.sub_sub_category_name
FROM `wallpaper_tbl_item` wi
INNER JOIN wallpaper_tbl_category wc ON wi.category_id = wc.category_id
INNER JOIN wallpaper_tbl_sub_category wsc ON wi.sub_category_id = wsc.sub_category_id
INNER JOIN wallpaper_tbl_sub_sub_category wssc ON wi.sub_sub_category_id = wssc.sub_sub_category_id
WHERE
wi.item_id = '" . addslashes(stripslashes($sub[$k]['item_id'])) . "'
AND wi.category_id = '" . addslashes(stripslashes($sub[$k]['category_id'])) . "'
AND wi.sub_category_id = '" . addslashes(stripslashes($sub[$k]['sub_category_id'])) . "'
AND wi.sub_sub_category_id = '" . addslashes(stripslashes($sub[$k]['sub_sub_category_id'])) . "'
";
$result = mysql_query($query) or die(mysql_error());
$imgInfo = mysql_fetch_assoc($result);
$imgHref = 'http://www.mydomain.com/' . str_replace(' ','_', $imgInfo['category_name']) . '/' . $imgInfo['sub_category_name'] . '/' . $imgInfo['sub_sub_category_name'] . '/' . str_replace(' ','_', $imgInfo['item_name']);

/*$img3=$csv->usersSearchOrder("wallpaper_tbl_sub_sub_category",array( 'sub_category_id' => $img2[0][sub_category_id]),'*','','rand()');
echo "<br />subsubcategory_name - ".$img3[$k][sub_sub_category_name];
$sub=$csv->usersSearchOrder("wallpaper_tbl_item",array('sub_sub_category_id' => $img3[$k][sub_sub_category_id]),'*','4','');
echo "<br />item name - ".$sub[$k][item_name]."<br />";*/
/*$img1=$csv->usersSearch("wallpaper_tbl_sub_sub_category",array( 'category_id' => $sub[$k][category_id]),'*','4','rand()');*/
?>


<div style="width:200px;" class="flt_lft sublist">
<a href="<?php echo $imgHref; ?>">
<?php
if($sub[$k][image]!='')
{ $imgsc=$sub[$k][image];
}else{
$imgsc="no_cat.jpg";
}
?>
<?php /*?><img src="../../image.php?width=200&height=150&image=<?php echo $img_root; ?>/item_img/800_600/<?php echo $imgsc; ?>" title="<?php echo $sub[$k][item_name] ?>" class="photo_bg" alt="" border="0" /><?php */?>


<img src="/wall/<?php echo $img1[0][category_name].'/'.$img2[0][sub_category_name].'/'.$img3[0][sub_sub_category_name].'/thumb/'.$imgsc;?>" title="<?php echo $sub[$k][item_name] ?>" class="photo_bg" alt="" border="0" />
</a>
<br />
<a href="<?php echo $_REQUEST['cname'];?>/<?php echo str_replace(' ','_',$sub[$k][item_name]); ?>"><?php echo $sub[$k][item_name]; ?></a>
</div>

<div style="width:25px;" class="flt_lft"><img src="../../images/spacer.gif" alt="" width="15" /></div>
<?php
if(($k+1)%4==0)
{
echo '<div class="clr"><img src="../../images/spacer.gif" alt="" /></div>';
echo '</div><div><img src="../../images/spacer.gif" alt="" height="15"/></div><div>';
}
?>
<?php }
/*else:
echo "fail";
endif;*/
?>

rocknbil

4:45 pm on Dec 16, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think you're going to have to look into image.php, as it probably has the same problem described below.

Get rid of all these

../../image.php

Change them to root-relative paths. What do I mean by this? Let's say your structure is like this.

[domain root]

image.php
includes (directory)
images (directory)
images/listings (directory)

And let's say you have the following files

includes/my-display.php
includes/config.php
images/spacer.gif
images/listings/listing1234.jpg
images/listings/listing5678.jpg

For URL/URL's - that is, page output, use the root relative path.

/includes/my-display.php
/images/spacer.gif
/images/listings/listing1234.jpg
/images/listings/listing5678.jpg

For PHP includes, use the full path, not the URL/URI.

include($_SERVER['DOCUMENT_ROOT']."/includes/config.php");

(you may need to rtrim the / off document root on some systems)

This is because an include is a file system include, not a URL. Most coders get lucky over and over again by using relative paths, and when it breaks they don't know why. A file system path is not the same as a URL/URI.

Why? Exactly what you're experiencing now . . . you (or the script you're using) get lost in the "toothpick syntax"

../../../

or, if you use rewrites, the browser may "think" you're in a certain directory when you're actually not.

So if image.php is at the root of the domain,

<img src="/image.php?width.....

And all those (ugh) spacer.gifs,

<img src="/images/spacer.gif" alt="" width="15" />

Now open image.php and look for the same thing. It's very likely that if you've called **this** script from some subdirectory, image.php may be still looking from that directory - but making all your image URL/URI's relative from root will always sort it out.

Same is true of calls to CSS or Javascript files.

zeus

10:49 pm on Dec 16, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



thanks for the reply buddy - I have no changed the parth it would be /images.php be cause those are in the root.

<?php
$img1=$csv->usersSearch("wallpaper_tbl_category",array( 'category_id' => 3),'*','');
$img2=$csv->usersSearchOrder("wallpaper_tbl_sub_category",array( 'category_id' => $img1[0][category_id]),'*','4','rand()');

/*echo "<br />Category name - ".$img1[0][category_name]."<br />
subcategory name - ".$img2[0][sub_category_name]."<br />";*/
if(empty($img2[0][sub_category_id])):
$random_id=rand(32,34);
$img2_id=$random_id;;
else:
$img2_id=$img2[0][sub_category_id];
endif;
$img3=$csv->usersSearchOrder("wallpaper_tbl_sub_sub_category",array( 'sub_category_id' => $img2_id),'*','','rand()');

/*if(empty($img3[0][sub_sub_category_id])):
$random_id=rand(5,8)
$img3_id=$random_id;
else:
$img3_id=$img2[0][sub_sub_category_id];
endif;*/
/*echo "<br />subsubcategory_name - ".$img3[0][sub_sub_category_name];*/
$sub=$csv->usersSearchOrder("wallpaper_tbl_item",array('sub_sub_category_id' => $img3[0][sub_sub_category_id]),'*','4','rand()');
/*echo "<br />item name - ".$sub[0][item_name]."<br />";*/
?>
<div style="padding:8px 60px;" align="center">

<div>
<?php
/*if(count($sub)==4):*/
// var_dump($sub);
for($k=0;$k<count($sub);$k++)
{
$query = "
SELECT wi.item_name, wc.category_name, wsc.sub_category_name, wssc.sub_sub_category_name
FROM `wallpaper_tbl_item` wi
INNER JOIN wallpaper_tbl_category wc ON wi.category_id = wc.category_id
INNER JOIN wallpaper_tbl_sub_category wsc ON wi.sub_category_id = wsc.sub_category_id
INNER JOIN wallpaper_tbl_sub_sub_category wssc ON wi.sub_sub_category_id = wssc.sub_sub_category_id
WHERE
wi.item_id = '" . addslashes(stripslashes($sub[$k]['item_id'])) . "'
AND wi.category_id = '" . addslashes(stripslashes($sub[$k]['category_id'])) . "'
AND wi.sub_category_id = '" . addslashes(stripslashes($sub[$k]['sub_category_id'])) . "'
AND wi.sub_sub_category_id = '" . addslashes(stripslashes($sub[$k]['sub_sub_category_id'])) . "'
";
$result = mysql_query($query) or die(mysql_error());
$imgInfo = mysql_fetch_assoc($result);
$imgHref = 'http://www.mydomain.com/' . str_replace(' ','_', $imgInfo['category_name']) . '/' . $imgInfo['sub_category_name'] . '/' . $imgInfo['sub_sub_category_name'] . '/' . str_replace(' ','_', $imgInfo['item_name']);

/*$img3=$csv->usersSearchOrder("wallpaper_tbl_sub_sub_category",array( 'sub_category_id' => $img2[0][sub_category_id]),'*','','rand()');
echo "<br />subsubcategory_name - ".$img3[$k][sub_sub_category_name];
$sub=$csv->usersSearchOrder("wallpaper_tbl_item",array('sub_sub_category_id' => $img3[$k][sub_sub_category_id]),'*','4','');
echo "<br />item name - ".$sub[$k][item_name]."<br />";*/
/*$img1=$csv->usersSearch("wallpaper_tbl_sub_sub_category",array( 'category_id' => $sub[$k][category_id]),'*','4','rand()');*/
?>


<div style="width:200px;" class="flt_lft sublist">
<a href="<?php echo $imgHref; ?>">
<?php
if($sub[$k][image]!='')
{ $imgsc=$sub[$k][image];
}else{
$imgsc="no_cat.jpg";
}
?>
<?php /*?><img src="/image.php?width=200&height=150&image=<?php echo $img_root; ?>/item_img/800_600/<?php echo $imgsc; ?>" title="<?php echo $sub[$k][item_name] ?>" class="photo_bg" alt="" border="0" /><?php */?>


<img src="/wall/<?php echo $img1[0][category_name].'/'.$img2[0][sub_category_name].'/'.$img3[0][sub_sub_category_name].'/thumb/'.$imgsc;?>" title="<?php echo $sub[$k][item_name] ?>" class="photo_bg" alt="" border="0" />
</a>
<br />
<a href="<?php echo $_REQUEST['cname'];?>/<?php echo str_replace(' ','_',$sub[$k][item_name]); ?>"><?php echo $sub[$k][item_name]; ?></a>
</div>

<div style="width:25px;" class="flt_lft"><img src="/images/spacer.gif" alt="" width="15" /></div>
<?php
if(($k+1)%4==0)
{
echo '<div class="clr"><img src="/images/spacer.gif" alt="" /></div>';
echo '</div><div><img src="/images/spacer.gif" alt="" height="15"/></div><div>';
}
?>
<?php }
/*else:
echo "fail";
endif;*/
?>


The site still looks ok, but link check still gives me those weird parth - in image.php I did no find any real image.php trouble like you mentioned.

zeus

12:18 am on Dec 18, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Dont bother anymore I have now removed the new version of my site, I think it has pure programming all over, but thanks anyway