A database for my data

From: Drew (X3N0PH0N) 6 Jun 2012 18:58
To: ANT_THOMAS 131 of 158
php code:
$blah = split(",", $row[images]);
foreach ($blah as $beep) {
echo "<img src=\"" . $beep . "\" />";
}


Probably a neater way which PB or Matt will come along and embarrass me with.
From: ANT_THOMAS 6 Jun 2012 19:29
To: Drew (X3N0PH0N) 132 of 158

And how do I pop that within a current big echo?

 

I'm trying (honest :$ )

From: ANT_THOMAS 6 Jun 2012 19:41
To: Drew (X3N0PH0N) 133 of 158
To help more, I'd like the images somewhere within this lot....

PHP code:
$car = $_GET['car'];

$query  = "SELECT * FROM cars WHERE carcode = '$car' ";
$result = mysql_query($query);

while($row = mysql_fetch_array($result, MYSQL_ASSOC))

echo "
<table>
    <tr>
    <th rowspan='5'><a href='#'><img src='./photos/{$row[thumb]}' /></a>
    <br />
    <td colspan='2' width='300px'><a href='car.php?car={$row[carcode]}'>{$row[title]}</a></td>
    </tr>
    <tr>
    <td width='250px'>{$row[year]}<br />
                      {$row[mileage]}<br />
                      {$row[engine]}<br />
                      {$row[fuel]}<br />
                      {$row[gearbox]}<br />     </td>
    <td width='250px'>{$row[colour]}<br />
                      {$row[mot]}<br />
                      {$row[tax]}<br /></td>
    </tr>
    <tr>
    <td colspan='2'>{$row[extra]}</td>
    </tr>
    <tr>
    <td colspan='2' align='right' valign='bottom'><b>&pound;{$row[price]}</b></td>
    </tr>
    <tr>
    <td colspan='2'>FULL WIDTH</td>
    </tr>
</table>

         ";
From: ANT_THOMAS 6 Jun 2012 19:49
To: ALL134 of 158

I think I'm doing this totally wrong, because I actually want to do some stuff with the output from the other rows depending on what it is rather than just showing their content.

 

Back to the drawing board possibly.

From: Drew (X3N0PH0N) 6 Jun 2012 19:50
To: ANT_THOMAS 135 of 158
Indicate where in that you'd want the images. But essentially you'd do...

code:
$blah = split(",", $row[images]);
foreach ($blah as $beep) {
$stringading .= "<img src=\"" . $beep . "\" />";
}


And then just add $stringading in where you want the images echoed. So like...

code:
$car = $_GET['car'];

$query  = "SELECT * FROM cars WHERE carcode = '$car' ";
$result = mysql_query($query);

while($row = mysql_fetch_array($result, MYSQL_ASSOC))

echo "
<table>
    <tr>
    <th rowspan='5'><a href='#'><img src='./photos/{$row[thumb]}' /></a>
    <br />
    <td colspan='2' width='300px'><a href='car.php?car={$row[carcode]}'>{$row[title]}</a></td>
    </tr>
    <tr>
    <td width='250px'>{$row[year]}<br />
                      {$row[mileage]}<br />
                      {$row[engine]}<br />
                      {$row[fuel]}<br />
                      {$row[gearbox]}<br />     </td>
    <td width='250px'>{$row[colour]}<br />
                      {$row[mot]}<br />
                      {$row[tax]}<br /></td>" . $stringading . "

</tr>
    <tr>
    <td colspan='2'>{$row[extra]}</td>
    </tr>
    <tr>
    <td colspan='2' align='right' valign='bottom'><b>&pound;{$row[price]}</b></td>
    </tr>
    <tr>
    <td colspan='2'>FULL WIDTH</td>
    </tr>
</table>

         ";


(which is a nonsensical place to dump them but you get the idea)

If you want more code around them, like table cells or whatever you'd do it in the foreach loop like:

code:
$blah = split(",", $row[images]);
$stringading = "tr";
foreach ($blah as $beep) {
$stringading .= "<td><img src=\"" . $beep . "\" /></td>";
}
$stringading .= "</tr>";
From: Drew (X3N0PH0N) 6 Jun 2012 19:56
To: Matt 136 of 158
Can't edit that ^ post for some reason. Can edit other posts fine, but trying to edit that one results in a long wait and then a generic beehive 'error: retry' page thing.
From: ANT_THOMAS 6 Jun 2012 20:09
To: Drew (X3N0PH0N) 137 of 158

That shows all the images (cheer)

 

But it breaks everything else and nothing else outputs :C

From: Matt 6 Jun 2012 20:46
To: Drew (X3N0PH0N) 138 of 158
So I see. It's getting stuck in a while loop which basically reads:

code:
function func_name()
{
    while (1) {

        if ($condition) {
            func_name();
        }

        if ($other_condition) {
            break; // exit the while loop
        }
    }
}


So it's recursively calling itself, in a infinite loop. It's code Andy wrote and bless his cotton socks he really did like using variables like $a, $b, $i, $j, $tmp, which while I'm sure have meaning to him mean fuck all to me.

Because I can't work it out, I'll remove it :Y

It's probably not important any how.
From: Drew (X3N0PH0N) 6 Jun 2012 21:08
To: Matt 139 of 158
:'D
From: Drew (X3N0PH0N) 6 Jun 2012 21:08
To: ANT_THOMAS 140 of 158
Paste your code.
From: Peter (BOUGHTONP) 6 Jun 2012 21:27
To: ANT_THOMAS 141 of 158
(Re-using an existing eighteen month old thread makes it more difficult to jump back to the start, and has no benefit when you're asking an entirely different question.)


Anyway, if you want to convert commas to image tags, then you can do it like this:

PHP code:
$string = '<img src="' . replace( ',' , '" /><img src="' , $string ) . '" />';


or like this:

PHP code:
$string = preg_replace( '(?:^|,)([^,]+)' , '<img src="$1" />' , $string );


(That one having the benefit of only needing to specifying the surrounding code once, although it may confuse people that don't understand regex.)


However, in most cases I'd probably do something similar to what Lucy posted (split and then foreach), because it makes things more flexible.
Any problems you're having there are most likely examples of why tables are not the right way to do layouts. :P
From: Peter (BOUGHTONP) 6 Jun 2012 21:30
To: Matt 142 of 158
Don't suppose that code was in some way responsible for code formatting?
From: ANT_THOMAS 6 Jun 2012 21:31
To: Peter (BOUGHTONP) 143 of 158

1. I will start a new thread next time I want coding help :C
2. I really need to learn how to use regex stuff.
3. Nothing wrong with tables (maybe). But I doubt they're the issue here.

From: ANT_THOMAS 6 Jun 2012 21:35
To: Drew (X3N0PH0N) 144 of 158
There you go..
PHP code:
<?php
mysql_connect("localhost","user","pass");
mysql_select_db("cogs");

$car = $_GET['car'];

$query  = "SELECT * FROM cars WHERE carcode = '$car' ";
$result = mysql_query($query);

while($row = mysql_fetch_array($result, MYSQL_ASSOC))

$blah = split(",", $row[images]);
foreach ($blah as $beep) {
$stringading .= "<img src=\"" . $beep . "\" />";
}

echo "

<table>
    <tr>
    <th rowspan='5'><a href='#'><img src='./photos/{$row[thumb]}' /></a>
    <br />
    " . $stringading . "
    <br />
    <td colspan='2' width='300px'><a href='car.php?car={$row[carcode]}'>{$row[title]}</a></td>
    </tr>
    <tr>
    <td width='250px'>Year - {$row[year]}<br />
                      Mileage - {$row[mileage]}<br />
                      Engine - {$row[engine]}<br />
                      Fuel - {$row[fuel]}<br />
                      Gearbox - {$row[gearbox]}<br />     </td>
    <td width='250px'>Body/Doors - <br />
                      Colour - {$row[colour]}<br />
                      MOT - {$row[mot]}<br />
                      Tax - {$row[tax]}<br /></td>
    </tr>
    <tr>
    <td colspan='2'>{$row[extra]}</td>
    </tr>
    <tr>
    <td colspan='2' align='right' valign='bottom'><b>&pound;{$row[price]}</b></td>
    </tr>
    <tr>
    <td colspan='2'>FULL WIDTH</td>
    </tr>
</table>

         ";
?>
From: Peter (BOUGHTONP) 6 Jun 2012 21:44
To: ANT_THOMAS 145 of 158
Everyone really needs to learn regex, though it seems to be eternally relegated to people's todo list. :(

Anyway, looking at the latest posted code, problem is that the while loop doesn't have a brace block - when it was a single statement (echo) it worked, but now it's multiple statements you need to group them.

PHP code:
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$blah = split(",", $row[images]);
foreach ($blah as $beep)
{
$stringading .= "<img src=\"" . $beep . "\" />";
}

echo "
...
";
}
?>
From: ANT_THOMAS 6 Jun 2012 21:47
To: Peter (BOUGHTONP) 146 of 158

I agree on the regex front. It seems to have been the fix for loads of stuff I've asked on here.

 

Also (hug) the extra braces worked!

From: Matt 6 Jun 2012 22:23
To: Peter (BOUGHTONP) 147 of 158
It is responsible for adding paragraph tags but obeying valid HTML nesting and tag ordering.
From: Peter (BOUGHTONP) 6 Jun 2012 22:51
To: Matt 148 of 158
Hmm, well must be something else that is stopping PHP being all coloured and stuff then?

In my previous post, there's extra line-breaks there, but in Ant's there aren't any. Might also be related in some way?
From: ANT_THOMAS 6 Jun 2012 22:54
To: Peter (BOUGHTONP) 149 of 158
I disabled them because they were being added extra within the code tags. If I hadn't it would've looked like yours.
From: Peter (BOUGHTONP) 6 Jun 2012 23:10
To: ANT_THOMAS 150 of 158

Ah, thought you might have done. However, it shouldn't be necessary - the line breaks shouldn't get added at all - content between code isn't supposed to be changed (excluding font/colour formatting).

 

Dunno what the cause is though - I did go look at the code Matt referred to and it's far too much effort trying to decipher it all, especially as tired as I currently am.

EDITED: 6 Jun 2012 23:10 by BOUGHTONP