开发者

Echo out arrays as links?

开发者 https://www.devze.com 2023-03-31 12:59 出处:网络
I would like to take my result from a SQL 开发者_开发知识库query, which I got in the form of arrays, and echo these out as the final step in a \"search engine\". I would also like these arrays to be l

I would like to take my result from a SQL 开发者_开发知识库query, which I got in the form of arrays, and echo these out as the final step in a "search engine". I would also like these arrays to be links, to the pages that I've set up for each information block, that the SQL query collects from my DB.

Something like this:

return $rows;
if (count($row > 0) {
foreach $rows as $row {
echo $row;
} }

Am I approaching this in the right way, how would I go on from there?

Any help appreciated.


Supposing $rows holds the information from your database as an Array, $row['link'] contains the link and $row['name'] the name you could do the following:

if(count($rows) > 0){

    foreach($rows as $row){
        echo '<a href="'.$row['link'].'">'.$row['name'].'</a>';
    }

}else{
    echo 'No results found.';
}


If the $rows variable merely contans a list of urls, you can tuse the following:

foreach ($rows as $row) echo "<a href='$row'>$row</a>";

Also you should remove that return $rows; from the top of the snipplet in your question, otherwise it will never reach the loop.


Your code is a bit mangled (you have a return statement before the rest of the logic, so it'll never get run. And you don't need the if statement.)

But in general, this principle might work; it depends on what your aim is. Of course, if you're printing these out as HTML, you'll need to embed them in some actual HTML, e.g.:

echo "<a href='$row'>Some text</a>";
0

精彩评论

暂无评论...
验证码 换一张
取 消