Jump to content


Photo

MYSQL Arrays!


  • Please log in to reply
3 replies to this topic

#1 xyn

xyn
  • Members
  • PipPipPip
  • Advanced Member
  • 779 posts
  • LocationNorthampton

Posted 07 October 2006 - 10:00 AM

Hey
Is there a way of fetching an array from MYSQL to get something
like; $row[field_name][1]
where as:
[field_name] being the field name
[1] being table row 1.

Basically I am having problems arranging my table, it's hard to explain
because i don't want to list the informatino in rows, I want to list it
4 rows across and 2 rows down :/ Check my website for what i mean.
.. http://www.eumod.co....index.php?x=bio

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,022 posts

Posted 07 October 2006 - 10:40 AM

See http://www.php.net/mysql_result

eg
$val = mysql_result($result, $row, 'field');

Although there are other ways of doing it, such as CSS or tables (see FAQ)
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 xyn

xyn
  • Members
  • PipPipPip
  • Advanced Member
  • 779 posts
  • LocationNorthampton

Posted 07 October 2006 - 10:47 AM

Well i had a look and it's all confusing to me :S
The part i'm unsure about is how it will work
in my pageination script...

PAGE SCRIPT:
<p><span class="heading">Eumod UK Biographies</span><br>
<span class="text">Welcome to the Eumod UK Biographies, these bios' where created by the vehicle 
original owners, Click the thumbnail for the main biography view, Members may 
leave reviews, rate the biography, and send the biography to their friends. 
Unregistered members must register for these special privileges, it's 100% free 
and will not take any longer than 2 minutes.</span></p>
<?PHP
//
//Set Limit
//
$limit = "8";

//
//Count & Collect
//
$SQL = mysql_query("SELECT count(*) FROM bio WHERE bio_auth='y' AND bio_done='y'");
$NUM = mysql_num_rows($SQL);

//
//Check Page
//
if(empty($page))
{
	$page="1";
}

//
//Page Values
//
$pgval = $page * $limit - ($limit);

//
//Limit SQL
//
$SQL2 = mysql_query("SELECT * FROM bio WHERE bio_auth='y' AND bio_done='y' LIMIT $pgval, $limit");
if(mysql_num_rows($SQL2) >0)
{	//Table Start
	echo 'table';
	//Row: 1
	while( $data = mysql_fetch_rows($SQL2))
{
	echo 'rows';
}	//End Table
	echo 'tableend';
}
else
{
	echo 'null';
}

//
//Set PREVIOUS
//
if($page != "1")
{
	$prev = $page--;
	echo "[ <a href=\"".$_SERVER['PHP_SELF']."&page=$prev\">Prev</a> | ";
}
else
{
	echo "Prev | ";
}

//
//Collect Pages
//
$pgnum = $SQL2 / $limit;
for($i=1;$i <= $pgnum; $i++)
{
	if( $i == "$page")
{
	echo $i. " : ";
}
else
{
	echo "<a href=\"".$_SERVER['PHP_SELF']."&page=$i\">$i</a> | ";
}
if(($SQL2 % $limit) != 0)
{
	if($i == "$page")
{
	echo $i." | ";
}
else
{
	echo "<a href=\"".$_SERVER['PHP_SELF']."&page=$i\">$i</a> | ";
}

//
//Next Page
//
if(($SQL2 - ($limit * $page) >0 )
{
	$nextpg = $page++;
	echo "<a href=\"".$_SERVER['PHP_SELF']."&page=$nextpage\">Next</a> ]";
}
else
{
	echo $i."Next ]";
}
?>

TABLES:
[code=php:0]<div align="center">
  <center>
  <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="470">
    <tr>
      <td width="110">
      <table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="110" height="104">
        <tr>
          <td width="100%">
          <p align="center">nnnn</p>
          </td>
        </tr>
      </table>
      </td>
      <td width="10">&nbsp;</td>
      <td width="110">
      <table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="110" height="104">
        <tr>
          <td width="100%">
          <p align="center">nnnn</p>
          </td>
        </tr>
      </table>
      </td>
      <td width="10">&nbsp;</td>
      <td width="110">
      <table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="110" height="104">
        <tr>
          <td width="100%">
          <p align="center">nnnn</p>
          </td>
        </tr>
      </table>
      </td>
      <td width="10">&nbsp;</td>
      <td width="110">
      <table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="110" height="104">
        <tr>
          <td width="100%">
          <p align="center">nnnn</p>
          </td>
        </tr>
      </table>
      </td>
    </tr>
    <tr>
      <td width="110" height="10"></td>
      <td width="10" height="10"></td>
      <td width="110" height="10"></td>
      <td width="10" height="10"></td>
      <td width="110" height="10"></td>
      <td width="10" height="10"></td>
      <td width="110" height="10"></td>
    </tr>
    <tr>
      <td width="110" height="24">
      <table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="110" height="24">
        <tr>
          <td width="100%">
          <p align="center">Heading</p>
          </td>
        </tr>
      </table>
      </td>
      <td width="10" height="24">&nbsp;</td>
      <td width="110" height="24">
      <table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="110" height="24">
        <tr>
          <td width="100%">
          <p align="center">Heading</p>
          </td>
        </tr>
      </table>
      </td>
      <td width="10" height="24">&nbsp;</td>
      <td width="110" height="24">
      <table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="110" height="24">
        <tr>
          <td width="100%">
          <p align="center">Heading</p>
          </td>
        </tr>
      </table>
      </td>
      <td width="10" height="24">&nbsp;</td>
      <td width="110" height="24">
      <table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="110" height="24">
        <tr>
          <td width="100%">
          <p align="center">Heading</p>
          </td>
        </tr>
      </table>
      </td>
    </tr>
    <tr>
      <td width="470" colspan="7" height="10"></td>
    </tr>
    <tr>
      <td width="110">
      <table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="110" height="104">
        <tr>
          <td width="100%">
          <p align="center">nnnn</p>
          </td>
        </tr>
      </table>
      </td>
      <td width="10">&nbsp;</td>
      <td width="110">
      <table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="110" height="104">
        <tr>
          <td width="100%">
          <p align="center">nnnn</p>
          </td>
        </tr>
      </table>
      </td>
      <td width="10">&nbsp;</td>
      <td width="110">
      <table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="110" height="104">
        <tr>
          <td width="100%">
          <p align="center">nnnn</p>
          </td>
        </tr>
      </table>
      </td>
      <td width="10">&nbsp;</td>
      <td width="110">
      <table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="110" height="104">
        <tr>
          <td width="100%">
          <p align="center">nnnn</p>
          </td>
        </tr>
      </table>
      </td>
    </tr>
    <tr>
      <td width="110" height="10"></td>
      <td width="10" height="10"></td>
      <td width="110" height="10"></td>
      <td width="10" height="10"></td>
      <td width="110" height="10"></td>
      <td width="10" height="10"></td>
      <td width="110" height="10"></td>
    </tr>
    <tr>
      <td width="110" height="24">
      <table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="110" height="24">
        <tr>
          <td width="100%">
          <p align="center">Heading</p>
          </td>
        </tr>
      </table>
      </td>
      <td width="10" height="24">&nbsp;</td>
      <td width="110" height="24">
      <table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="110" height="24">
        <tr>
          <td width="100%">
          <p align="center">Heading</p>
          </td>
        </tr>
      </table>
      </td>
      <td width="10" height="24">&nbsp;</td>
      <td width="110" height="24">
      <table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="110" height="24">
        <tr>
          <td width="100%">
          <p align="center">Heading</p>
          </td>
        </tr>
      </table>
      </td>
      <td width="10" height="24">&nbsp;</td>
      <td width="110" height="24">
      <table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="110" height="24">
        <tr>
          <td width="100%">
          <p align="center">Heading</p>
          </td>
        </tr>
      </table>
      </td>
    </tr>
  </table>
  </center>
</div>


#4 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 07 October 2006 - 12:54 PM

Normally you create helper functions, when dynamic content logic is needed, the helper functions create the arrays of rows and columns formatted to fit your display, so if you don't have enough data the helper functions can stop another row from being displayed or fill empty elements when there is room for another row, but not enough elements to fill the entire row.

You could do this in your db result loop, but it gets messy and will always have to be changed is your row or column logic changes, so creating helper functions that can handle any amount or different types of (row and column) displays will be better, plus you will have reusable code that you can use anytime you need this kind of logic in your other applications!

Also this...

$SQL = mysql_query("SELECT count(*) FROM bio WHERE bio_auth='y' AND bio_done='y'");
$NUM = mysql_num_rows($SQL);


Will always return (1) row for $NUM, because mysql_num_rows returns how many rows were returned, which will always be (1) when using COUNT()! Use mysql_result or mysql_fetch_..., to get the value for COUNT()!


me!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users