Changing value to text from a SQL column and printing it.

Hi everyone.
I want change the value (which in my case is 0) from the database column to text, let’s say ROB. Then print it. Currently I have this code:

while($row = mysql_fetch_row($query)) {
echo “

”;
echo ‘’ . $row[0] . ‘’;
echo ‘’ . $row[1] . ‘’;
echo ‘’ . $row[2] . ‘’;
echo ‘’ . $row[3] . ‘’;
echo ‘’ . $row[4] . ‘’;
echo “\n”;
}

I’ve tried with if statement, but apparently - it didn’t work.
Thanks.

not quite sure i follow what you want to change…
i can see from your post you are trying to display 5 items, however you don’t mention which one (or maybe all?) so i’ve written a little snippet to show you how to change a value.

to change ‘0’ to ‘Rob’ you can do something like this:
[php]

<?php $oldVal = 0; $newVal = str_replace(0, 'Rob', $oldVal); // check $oldVal for an instance of 0 if found change it to Rob echo $oldVal; // outputs 0 echo $newVal; // outputs Rob ?>

[/php]

Hope that helps, if not re-post with a bit more info…
:wink:

Had problems registering…

Anyway, this didn’t worked well… What did I do wrong? :confused:

while($row = mysql_fetch_row($query)) { $oldVal = $row[3]; if $oldVal == '0' {$newVal = str_replace(0, 'Rob', $oldVal);} echo "<tr>"; echo '<td>' . $row[0] . '</td>'; echo '<td>' . $row[1] . '</td>'; echo '<td>' . $row[2] . '</td>'; echo '<td>' . $row[3] . '</td>'; echo '<td>' . $row[4] . '</td>'; echo "</tr>\n"; }

nearly…

The code i gave was an example, it didn’t fit ‘straight into’ your code,

However, a little change like this is all thats needed:
[php]while($row = mysql_fetch_row($query))
{
if $row[3] == ‘0’ {$row[3] = str_replace(0, ‘Rob’, $row[3]);}
echo “

”;
echo ‘’ . $row[0] . ‘’;
echo ‘’ . $row[1] . ‘’;
echo ‘’ . $row[2] . ‘’;
echo ‘’ . $row[3] . ‘’;
echo ‘’ . $row[4] . ‘’;
echo “\n”;
}
[/php]

Hope this helps,
:wink:

Thanks, but it still isn’t working. Gives a blank page. :confused:

sorry, my mistake :-[ :-[

i forgot the braces around this:
[php] if $row[3] == ‘0’[/php]

should be:
[php] if ($row[3] == ‘0’) [/php]

:wink:

Nice, it’s working. Haven’t noticed the mistake myself too… Learning Pascal at school, starting to do mistakes in C++ occasionally too. :confused:

Thank you!

Eh, one other aspect isn’t working as I would like it to. What if it’s replacement like this:

[php]while($row = mysql_fetch_row($query)) {
if ($row[3] == ‘disali_6:5:2_9’) {$row[3] = str_replace(0, ‘Rob’, $row[3]);}
echo “

”;
echo ‘’ . $row[0] . ‘’;
echo ‘’ . $row[1] . ‘’;
echo ‘’ . $row[2] . ‘’;
echo ‘’ . $row[3] . ‘’;
echo ‘’ . $row[4] . ‘’;
echo “\n”;[/php]

It cut’s it on : and _…

Sorry, noticed one mistake. Everything is working again now. Can’t edit my posts and can’t give karma yet though. :confused: Sorry for triple post, these two can be deleted.

Thanks again.

Hi,

I’m happy to of helped you get it working, :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service