Jump to content

PHP SQL Table Array


preanorian

Recommended Posts

Hello I am a newcomer to PHP and i am learning by trial and error but i have run into a wall thus i need some help from the great minds in here.

 

My problem is the following:

 

I want to bring in images and title off the SQL and array them in sets of four(4) then afterward I want a new row to star and show the next 4 columns

 

EX

 

image 1

image 2

image 3

image 3

image 5

image 6

image 7

image 8

 

Something along those lines. I have no problems bringing in the data its just the array thats driving me crazy!

 

Thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/206086-php-sql-table-array/
Share on other sites

Hi

 

In php a multidimensional array is an array of arrays.

 

So you want something like:-

 


$AllImages = array();
$ImageCnt = 0;

while ($row = mysql_fetch_assoc($rs))
{
$ImageCnt++
$FourImages[] = $row['SomeField'];
if ($ImageCnt % 4 == 0)
{
$AllImages[] = $FourImages;
$FourImages = array();
}
}
if ($ImageCnt % 4 != 0)
{
$AllImages[] = $FourImages;
}

 

Hope that helps.

 

All the best

 

Keith

Link to comment
https://forums.phpfreaks.com/topic/206086-php-sql-table-array/#findComment-1078310
Share on other sites

Not tested, so there might be some syntax errors. BUt, logic shoudl be sound.

<?php
//Variable to determine number of columns
$records_per_row = 4;

//Function for creating a row
function create_row($row_data)
{
    if(count($row_data)<1) { return false; }
    $output = "<tr>\n";
    foreach($row_data as $record)
    {
        $output = "<td><img src=\"{$record['src']}\" /><br />{$record['name']}</td>\n";
    }
    $output = "</tr>\n";
    return $output;
}

//Create & run query
$query = "SELECT name, src FROM images";
$result = mysql_query($query);

//Process the data
$records = array();
$tableOutput = '';
while($records[] = mysql_fetch_assoc($result))
{
    if(count($records)==$records_per_row)
    {
        //Create row HTML and reset array
        $tableOutput .= create_row($records);
        $records = array();
    }
}
//Process last row
$tableOutput .= create_row($records);

?>
<html>
<body>

<table>
<?php echo $tableOutput; ?>
</table>

</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/206086-php-sql-table-array/#findComment-1078315
Share on other sites

Hello Keith and Mjdamato Thanks for the help but sadly I am not getting the concept and or I am messing up on something. I have attached the page code so you can get an idea of what I am working with.

 

It is a very old and messy code / page that needs some revamping. Sadly I do not have the skills yet to re write the entire thing so I just wanted to use the old and add in some new php to it.

 

I believe that the "Echo" is whats causing the trouble but don't know how to get around it

 

-------------------------------------------------------------------




<?

if(!isset($_GET[lett])) { $let = "(LEFT(title,1) REGEXP '[0-Z]')"; }
if($_GET[lett] == "a") { $let = "(LEFT(title,1) REGEXP '[a]')"; }
if($_GET[lett] == "b") { $let = "(LEFT(title,1) REGEXP '[b]')"; }
if($_GET[lett] == "c") { $let = "(LEFT(title,1) REGEXP '[c]')"; }
if($_GET[lett] == "d") { $let = "(LEFT(title,1) REGEXP '[d]')"; }
if($_GET[lett] == "e") { $let = "(LEFT(title,1) REGEXP '[e]')"; }
if($_GET[lett] == "f") { $let = "(LEFT(title,1) REGEXP '[f]')"; }
if($_GET[lett] == "g") { $let = "(LEFT(title,1) REGEXP '[g]')"; }
if($_GET[lett] == "h") { $let = "(LEFT(title,1) REGEXP '[h]')"; }
if($_GET[lett] == "i") { $let = "(LEFT(title,1) REGEXP '[i]')"; }
if($_GET[lett] == "j") { $let = "(LEFT(title,1) REGEXP '[j]')"; }
if($_GET[lett] == "k") { $let = "(LEFT(title,1) REGEXP '[k]')"; }
if($_GET[lett] == "l") { $let = "(LEFT(title,1) REGEXP '[l]')"; }
if($_GET[lett] == "m") { $let = "(LEFT(title,1) REGEXP '[m]')"; }
if($_GET[lett] == "n") { $let = "(LEFT(title,1) REGEXP '[n]')"; }
if($_GET[lett] == "o") { $let = "(LEFT(title,1) REGEXP '[o]')"; }
if($_GET[lett] == "p") { $let = "(LEFT(title,1) REGEXP '[p]')"; }
if($_GET[lett] == "q") { $let = "(LEFT(title,1) REGEXP '[q]')"; }
if($_GET[lett] == "r") { $let = "(LEFT(title,1) REGEXP '[r]')"; }
if($_GET[lett] == "s") { $let = "(LEFT(title,1) REGEXP '[s]')"; }
if($_GET[lett] == "t") { $let = "(LEFT(title,1) REGEXP '[t]')"; }
if($_GET[lett] == "u") { $let = "(LEFT(title,1) REGEXP '[u]')"; }
if($_GET[lett] == "v") { $let = "(LEFT(title,1) REGEXP '[v]')"; }
if($_GET[lett] == "w") { $let = "(LEFT(title,1) REGEXP '[w]')"; }
if($_GET[lett] == "x") { $let = "(LEFT(title,1) REGEXP '[x]')"; }
if($_GET[lett] == "y") { $let = "(LEFT(title,1) REGEXP '[y]')"; }
if($_GET[lett] == "z") { $let = "(LEFT(title,1) REGEXP '[z]')"; }



$page_list = "SELECT *
FROM `My_Database`
WHERE 
{$let}
ORDER BY title ASC";


?>
<table bgcolor="#7591AC" background="http://www.mypage.com/images/cpanel/main/BG_stripe.png"  width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><table width="100%" border="0" cellspacing="10" cellpadding="10">
        <tr>
          <td bgcolor="#FFFFFF"> Banner </td>
        </tr>
      </table>
      <table width="100%" border="0" cellspacing="10" cellpadding="10">
        <tr>
          <td bgcolor="#FFFFFF"><?

echo "<table width=\"100%\" bgcolor=\"#505556\" border=\"0\" cellpadding=\"5\"><tr><td><center>

<a href=\"http://www.mysite.com/index.php?page=my_page\" class=\"url_white_4B\">All</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=a\" class=\"url_white_4B\">A</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=b\" class=\"url_white_4B\">B</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=c\" class=\"url_white_4B\">C</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=d\" class=\"url_white_4B\">D</a> -

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=e\" class=\"url_white_4B\">E</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=f\" class=\"url_white_4B\">F</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=g\" class=\"url_white_4B\">G</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=h\" class=\"url_white_4B\">H</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=i\" class=\"url_white_4B\">I</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=j\" class=\"url_white_4B\">J</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=k\" class=\"url_white_4B\">K</a> -

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=l\" class=\"url_white_4B\">L</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=m\" class=\"url_white_4B\">M</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=n\" class=\"url_white_4B\">N</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=o\" class=\"url_white_4B\">O</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=p\" class=\"url_white_4B\">P</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=q\" class=\"url_white_4B\">Q</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=r\" class=\"url_white_4B\">R</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=s\" class=\"url_white_4B\">S</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=t\" class=\"url_white_4B\">T</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=u\" class=\"url_white_4B\">U</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=v\" class=\"url_white_4B\">V</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=w\" class=\"url_white_4B\">W</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=x\" class=\"url_white_4B\">X</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=y\" class=\"url_white_4B\">Y</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=z\" class=\"url_white_4B\">Z</a>

</center></td></tr></table><br />";


?>
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td><?

function add_stars($rating) {
switch ($rating)
{
case 10: $stars = "http://www.mysite.com/_content/images/STARS/5Stars.png"; break;
case 9: $stars = "http://www.mysite.com/_content/images/STARS/4_5Stars.png"; break;
case 8: $stars = "http://www.mysite.com/_content/images/STARS/4Stars.png"; break;
case 7: $stars = "http://www.mysite.com/_content/images/STARS/3_5Stars.png"; break;
case 6: $stars = "http://www.mysite.com/_content/images/STARS/3Stars.png"; break;
case 5: $stars = "http://www.mysite.com/_content/images/STARS/2_5Stars.png"; break;
case 4: $stars = "http://www.mysite.com/_content/images/STARS/2Stars.png"; break;
case 3: $stars = "http://www.mysite.com/_content/images/STARS/1_5Stars.png"; break;
case 2: $stars = "http://www.mysite.com/_content/images/STARS/1Stars.png"; break;
case 1: $stars = "http://www.mysite.com/_content/images/STARS/_55tars.png"; break;
case 0: $stars = "http://www.mysite.com/_content/images/STARS/0Stars.png"; break;
} return $stars;}


$result_list = $Db->query($page_list);

while ($data = mysql_fetch_array($result_list)) {




echo " [color=orange]NEW Multidemnsional array here[/color] "; } ?></td>
              </tr>
            </table>
            <?

echo "<table width=\"100%\" bgcolor=\"#505556\" border=\"0\" cellpadding=\"5\"><tr><td><center>

<a href=\"http://www.mysite.com/index.php?page=my_page\" class=\"url_white_4B\">All</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=a\" class=\"url_white_4B\">A</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=b\" class=\"url_white_4B\">B</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=c\" class=\"url_white_4B\">C</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=d\" class=\"url_white_4B\">D</a> -

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=e\" class=\"url_white_4B\">E</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=f\" class=\"url_white_4B\">F</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=g\" class=\"url_white_4B\">G</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=h\" class=\"url_white_4B\">H</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=i\" class=\"url_white_4B\">I</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=j\" class=\"url_white_4B\">J</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=k\" class=\"url_white_4B\">K</a> -

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=l\" class=\"url_white_4B\">L</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=m\" class=\"url_white_4B\">M</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=n\" class=\"url_white_4B\">N</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=o\" class=\"url_white_4B\">O</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=p\" class=\"url_white_4B\">P</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=q\" class=\"url_white_4B\">Q</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=r\" class=\"url_white_4B\">R</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=s\" class=\"url_white_4B\">S</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=t\" class=\"url_white_4B\">T</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=u\" class=\"url_white_4B\">U</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=v\" class=\"url_white_4B\">V</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=w\" class=\"url_white_4B\">W</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=x\" class=\"url_white_4B\">X</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=y\" class=\"url_white_4B\">Y</a> - 

<a href=\"http://www.mysite.com/index.php?page=my_page&lett=z\" class=\"url_white_4B\">Z</a>

</center></td></tr></table><br />";


?></td>
        </tr>
      </table></td>
  </tr>
</table>



 

-------------------------------------------------------------------------------

 

 

None the less I am very grateful for your help!

Link to comment
https://forums.phpfreaks.com/topic/206086-php-sql-table-array/#findComment-1078811
Share on other sites

Hi

 

Don't think you need to put it into an array.

 

Replace the following code

 

$result_list = $Db->query($page_list);

while ($data = mysql_fetch_array($result_list)) {

echo " [color=orange]NEW Multidemnsional array here[/color] "; } 

 

with (replace the field name).

 

<?php

$result_list = $Db->query($page_list);

while ($data = mysql_fetch_array($result_list)) 
{
$ImageCnt++
echo "<img src='".$data['SomeFieldNameGivingTheImageUrl']."' />";
if ($ImageCnt % 4 == 0)
{
	echo "<br />";
}
}

?>

 

All the best

 

Keith

Link to comment
https://forums.phpfreaks.com/topic/206086-php-sql-table-array/#findComment-1078837
Share on other sites

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.