Forum Moderators: coopster

Message Too Old, No Replies

Hyperlink Problem Help

hyperlink

         

chrissim

11:25 am on May 4, 2010 (gmt 0)

10+ Year Member



hi,

i have managed to retrieve everything perfectly from my mysql database but the only problem the links were somehow gave me some hiccups here. i'm getting the link error with my site domain together with link like these below. Could it some coding error here?

<snip>


<td>
<table>
<tr valign="top">
<td></td>
<td>
<b><a href='<?=$row['link'] ?>'><?=$row['sitename'] ?></a></b><br />
</td>
<td width="25%">&nbsp;</td>
</tr>
</table>
</td>

[edited by: dreamcatcher at 7:21 am (utc) on May 5, 2010]
[edit reason] no urls please, see T.O.S [/edit]

Readie

11:50 am on May 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<?=$row['link'] ?>

Try replacing that with this:

<?php echo $row['link']; ?>

(And all similar)

Matthew1980

11:55 am on May 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there,

Yes try not to use the short tags, as not all servers support the use of short tags, and it is considered good practise to always use the full (<?php) tags. This will help avoid headaches later on ;-p

Cheers,
MRb

chrissim

12:17 pm on May 4, 2010 (gmt 0)

10+ Year Member



hi guys

Error still the same here but here's my complete script maybe you guys can figure it out what is the problem. Thanks


<?

$query = "SELECT * FROM linksites"; //
$result=mysql_query($query);
$cols=4;
echo "<table>";

do{
echo "<tr>";
for($i=1;$i<=$cols;$i++){
$row=mysql_fetch_array($result);
if($row){
?>
<td>
<table>
<tr valign="top">
<td><img src="image.src"></td>
<td>
<a href='<?php echo $row['link']; ?> '><?php echo $row['sitename'];?</a>
</td>
<td width="25%">&nbsp;</td>
</tr>
</table>
</td>
<?
}
else{
echo "<td>&nbsp;</td>";//If there are no more records at the end, add a blank column
}
}
} while($row);
echo "</table>";

?>

Matthew1980

12:37 pm on May 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there chrissim,

the code structure should be like this:-

$query = "yourQuery stuff";
$result=mysql_query($query);

//Start building table here
?>
<table>
<tr>
<?php
while($row = mysql_fetch_array($result)){
?>
Do your table building here
<td>
<a href='<?php echo $row['link']; ?> '><?php echo $row['sitename'];?</a>
</td>
<?php
}

?>
//close table here
</table>

Basically this is the guide you need when getting data:-

=>query
=>prepare div/table
=>open loop an iterate through each instance of data with a while loop
=>close loop
=>close div/table

Hopefully from that pseudo code should get you on the right track if you see where I am coming from :)

Cheers,
MRb

chrissim

1:22 pm on May 4, 2010 (gmt 0)

10+ Year Member



hi Matthew1980

Sorry i don't have any php background experience here but i hope someone like you can correct my script here. The script runs ok except this minor link that bother me eg [mysite.com...]

Hope it clarify :)

Thanks

Chris

chrissim

2:14 pm on May 4, 2010 (gmt 0)

10+ Year Member



Anyone ?

rocknbil

6:05 pm on May 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



i'm getting the link error


Looked the thread over three times and don't see: what is the "error"?

If it's what I *think* it is, it's "page not found." I'm going to use https in my examples because the software on this board is munging up the code in normal links. Look at what you are outputting:

[mysite.com...]

What does this mean? This means you are looking for a directory or subdomain www.yahoo.com inside the root of your domain. Doesn't exist, does it? [b]404 not found.
:-)

I can only extrapolate one of two things:

a) You intend to output links directly to yahoo.com.
b) You intend to pass this URL through "some script" to track the outbound links.

In either case, we need to know what the values of what "link" and "sitename" are. Not a fan of the start/stop html/php mix, my code is a bit different (and is typed on the fly, MAY CONTAIN SYNTAX errors.)

Note also that "if" will return a single result, and likely the last result, while will return all results in the query. This is a case for while.

Solution a, presuming value of both link and sitename are "www.yahoo.com"


<?php
$cols=4;
$colcount=0;
$output=null;
$query = "SELECT * FROM linksites";
$result=mysql_query($query);
while ($row=mysql_fetch_array($result)) {
if ($colcount==0) { $output .= '<tr>'; }
// Check to make sure LINK has http or https
$link = (preg_match('/^https*:\/\/.*/i',$row['link']))?$row['link']:'http://' . $row['link'];
$output .= '
<td><a href="' . $link . '">' . $row['sitename'] . '</a></td>
';
$colcount++;
if ($colcount >= $cols) { $output .= '</tr>'; $colcount=0; }
}
// now, if the last row ends unevenly, fill it out.
if ($output) {
if (($colcount > 0) and ($colcount < $cols)) {
for ($i=$colcount;$i<$cols;$i++) {
$output .= '<td>&nbsp;</td>';
}
$output .= '</tr>';
}
$output = "<table>$output</table>";
}
else { $output = '<p>No records to display.</p>'; }
echo $output;
?>


Solution b, making same presumption, but to pass everything through "some-tracker.php". The approach is exactly the same as above except for this line.

$output .= '
<td><a href="some-tracker.php?site=' . $link . '">' . $row['sitename'] . '</a></td>
';

Which will give you, minus the S in https,

some-tracker.php?site=https://www.yahoo.com

I may be off the mark as we're shooting in the dark here without the two bits of info I mentioned, but this may lead to enlightenment, if so . . . Namaste. :-)

mooger35

6:06 pm on May 4, 2010 (gmt 0)

10+ Year Member



Try this....

<?php
function display_table($data=array('No data available'), $cols="1", $class="class")
{
// add extra blank cells if needed
for($i=0;$i<(count($data)%$cols);$i++){
$data[] = '&nbsp;';
}
// split array into rows
$data_rows = array_chunk($data,$cols);
// start table output code
$display = "<table class=\"$class\">\r\n";
// output each row
foreach($data_rows as $tr){
$display .= "<tr>\r\n";
// output each cell within the row
foreach($tr as $td){
$display .= "<td>$td</td>\r\n";
}
// close row tag
$display .= "</tr>\r\n";
}
// close table tag
$display .= "</table>\r\n";
// return table
return $display;
}

// populate array
$links_array = array();

$query = "SELECT * FROM linksites";
$result=mysql_query($query);
while($row=mysql_fetch_assoc($result)){
$links_array[] = '<a href="'.$row['link'].'">'.$row['sitename'].'</a>';
}

// now echo table
echo display_table($links_array, 4, $class="links");
?>

then just add a class of "links" to your css file with setting td width and all other styles.

chrissim

11:49 pm on May 4, 2010 (gmt 0)

10+ Year Member




Thanks guys :) i give it a try and see it how it goes.

Thanks

Chris

mooger35

8:51 pm on May 5, 2010 (gmt 0)

10+ Year Member



Just realized I had an error in there...

final line should be:

echo display_table($links_array, 4, "links");