开发者

PHP function creates tables, how to add database ID as URL parameter?

I have some code that creates a table like this:

for($i=0; $i<$fields_num; $i++)
{
    $field = mysql_fetch_field($result);
    echo "<td" . $i;
    echo "></td>";
}

echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
    echo "<tr>";
    // $r开发者_高级运维ow is array... foreach( .. ) puts every element
    // of $row to $cell variable
    $counter = 0;
    foreach($row as $cell)
    {
        if($counter == 0)
        {//1st column
           echo "<td><strong>" .$cell. "</strong></td>";
        }
        elseif($counter == 8)
        {//7th column
            echo "<td><img src=\"http://php.alton.com/image.php/image.jpg?width=100&amp;height=100&amp;cropratio=1:1&amp;image=".$cell."\" /></td>";
        }
        elseif($counter == 9)
        {//eighth column
            echo "<td><strong>" .$cell. "</strong></td>";
        }
        else
            echo "<td>$cell</td>";
            //inside your default td
        $counter++;
    }
    echo "</tr>\n";
}

I am trying to get the MySQL result from column 0 (ID) and pass this variable into a URL parameter in the result. I want the SQL results in Column 8 to create a link that includes the ID from column 0. For example: "/details.php?ID=43" where the ID comes from the 1st column in the row. Does this make sense?


Your output code is inherently bad since it relies on the order the results are returned in from the database. This may work now, but is an incredibly fragile way of doing things. You should refer to your fields (columns) by name, not by the order they're returned in. You're also needlessly complicating your output code with that inner loop and if…else construct. Try something along these lines:

<?php while ($row = mysql_fetch_assoc($result)) : ?>
    <tr>
        (substitute 'col1' etc by the actual columns names)
        <td><strong><?php echo $row['col1']; ?></strong></td>
        <td><?php echo $row['col2']; ?></td>
        …
        <td>
            <img src="http://php.alton.com/image.php/image.jpg?width=100&amp;height=100&amp;cropratio=1:1&amp;image=<?php echo $row['image']; ?>" />
        </td>
        …
    </tr>
<?php endwhile; ?>

I'm not quite sure about your actual question, but approaching your problem like this may answer it already.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜