Jump to content

Need help with creating an array from sql query


juggernaut

Recommended Posts

Hi, normally I'd just search for answers but all I've found won't help me...

Dumb this down for me if you can as I'm kinda new to loops or creating them properly.

I want to create this array from a set of rows in a mysql table and show the array as html elements.

The rows include: $name, $url. The context I want to output is: <a href="$url">$name</a><br>

How do I call these variables and place them into this script? I've tried various methods and the result is the word "Array" over and over. I left the basic script to allow someone to easily modify it.

How do I get $items to show the above context? any help will be appreciated and a small reward offered for any help!


The script basically creates a set of evenly distributed columns based on the data in the array:

[code]
<?php

//Array
$items = array('1','2','3','4','5','6','7','8');

// Default # of Columns
$defcolumns = 4;

// Number of columns
$numcols = isset($_GET['columns']) && $_GET['columns']>0 ? $_GET['columns'] : $defcolumns;

// Number of columns + 1
$columnsplusone = $numcols + 1;

// Number of Items
$numitems = count($items);

// Number of Rows
$numrows = ceil($numitems/$numcols);

?>


<br>


<table border="0" width="500">
<th colspan="100%">Array Title</th>
<?php
        for ($row=1; $row <= $numrows; $row++)
        {
                // Initialize Cells
                $cell = 0;
?>
        <tr>
<?php
                for ($col=1; $col <= $numcols; $col++)
                {
?>
                <td width="25%" align="center">
<?php
        if ($col===1) {
        $cell += $row;
                print $items[$cell - 1];
    } else {
        $cell += $numrows;
                print $items[$cell - 1];
        }
?>
                </td>
<?php
                }
?>
        </tr>
<?php
        }
?>
</table>
[/code]

This is my first post, sorry if I've screwed something up! I read the rules.
If the data is from a database query, there are better ways of displaying the data without needing to dump it into an array.

In any event you will need to show us the query you are running or the format of the array you have created.
Hey mjdamato.

I'd appreciate a link to a better way of doing what I'm asking... which is 4 columns of data evenly distributed depending on the amount of links in my database.

Here's the query I want to run.

[code]
<?php
import_request_variables("gP", "val_");

$user_db = 'username';
$pass_db = 'password';
$host_db = 'localhost';
$db = 'database';

@mysql_connect ($host_db, $user_db, $pass_db);
@mysql_select_db ($db);

$table = 'tablename';

$result = mysql_query("SELECT * FROM $table WHERE id NOT IN ($val_sid) AND active = '1'");
?>
[/code]

The array looks like this:
[code]
$items = array('1','2','3','4','5','6','7','8');
[/code]

I want it to look like this based on the query results:
[code]
$items = array('<a href="$url">$name</a><br>','<a href="$url">$name</a><br>','<a href="$url">$name</a><br>','<a href="$url">$name</a><br>');
[/code]

Obviously ordered by the query or what I put in the query later on. I'm quite sure this is going to look very messy to someone with more experience, but this is the best I could come up with... Any links to tutorials to similar things would be appreciated.
The only part I'm unlear about is "4 columns of data evenly distributed". Do you mean you want the data displayed like this
[code]Link 1 | Link 6  | Link11 | Link16
Link 2 | Link 7  | Link12 | Link17
Link 3 | Link 8  | Link13 | Link18
Link 4 | Link 9  | Link14 | Link19
Link 5 | Link 10 | Link15 | Link20[/code]
How 'bout this:
[code]<?php

$columns = 4;

set_include_path('./includes');
require_once ("connectDB.php");

$query = "SELECT * FROM genres ORDER BY displayname";
$result = mysql_query($query) or die ("Query:<br>".$query."<br>Error:<br>".mysql_error());

$recCount = mysql_num_rows($result);

if ($recCount==0) {
    echo "There were no results.";
} else {
    echo "<table><tr>";

    $currentRec = 0;
    for ($col=1; $col<=$columns; $col++) {
        echo "<td valign=\"top\">";
        for ($i=0; $i<($recCount/$columns); $i++) {
            if ($currentRec<$recCount) {
                $record = mysql_fetch_assoc($result);
                echo "<a href=\"".$record['url']."\">".$record['name']."</a><br>";
                $currentRec++;
            }
        }
        echo "</td>";
    }
    echo "</tr></table>";
}
?>,/code][/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.