Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-03-2012, 10:49 PM   PM User | #1
jake66
New Coder

 
Join Date: Jul 2011
Posts: 23
Thanks: 8
Thanked 0 Times in 0 Posts
jake66 is an unknown quantity at this point
using data from mysql_fetch_array

Hi,
I have taken data from MySQL in the standard way and have $row['datefield'] etc to use in a table. Depending on a specific column value, some of the records are editable and so an edit link should be shown only next to those records in a separate column. I don't really understand how I can make this happen.
Here's my code
Code:
while($rows=mysql_fetch_array($result)){
	$rows['date'] = date( "d/m/Y", $rows['date_ts'] );
?>
<tr>
<td nowrap><? echo $rows['datefield']; ?></td>
<td nowrap><? echo $rows['shift']; ?></td>
<td nowrap> <? echo $rows['reason'];?></td>
<td nowrap> <? echo $rows['person_covering']; ?></td>
<td nowrap> &pound;<? echo $rows['cost']; ?></td>
<td nowrap> <? 	if(  substr( trim( $rows['staff_approved'] ), 0, 3)  === "Yes"){ }
else { ?> <a href="updaterecord.php?id=<? echo $rows['id'];?>">Edit<? }?>
I am checking whether the staff approved column starts with Yes and if it does not then showing an edit link. However this seems to show edit links for all of them....

Any help gratefully welcomed,
Jake

Last edited by jake66; 09-05-2012 at 08:45 PM.. Reason: resolved
jake66 is offline   Reply With Quote
Old 09-03-2012, 11:19 PM   PM User | #2
_Aerospace_Eng_
Supreme Master coder!


 
_Aerospace_Eng_'s Avatar
 
Join Date: Dec 2004
Location: In a place far, far away...
Posts: 19,292
Thanks: 2
Thanked 1,044 Times in 1,020 Posts
_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light
What kind of data is contained in 'staff_approved'? Why can't you just check the value rather than trying to do a substr?
PHP Code:
<?php
while($rows=mysql_fetch_array($result))
{
    
$rows['date'] = date"d/m/Y"$rows['date_ts'] );
?>
    <tr>
        <td nowrap><?php echo $rows['datefield']; ?></td>
        <td nowrap><?php echo $rows['shift']; ?></td>
        <td nowrap> <?php echo $rows['reason'];?></td>
        <td nowrap> <?php echo $rows['person_covering']; ?></td>
        <td nowrap> &pound;<?php echo $rows['cost']; ?></td>
        <td nowrap><?php if(strncasecmp(trim($rows['staff_approved']),'yes') != 0)
        {
            
?>
            <a href="updaterecord.php?id=<?php echo $rows['id'];?>">Edit</a>
            <?php
        
}
        
?>
        </td>
    </tr>
<?php
}
?>
__________________
||||If you are getting paid to do a job, don't ask for help on it!||||
_Aerospace_Eng_ is offline   Reply With Quote
Old 09-04-2012, 01:01 AM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,055 Times in 4,024 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
First of all, this makes no sense:
Code:
	$rows['date'] = date( "d/m/Y", $rows['date_ts'] );
        ...
     <td nowrap><? echo $rows['datefield']; ?></td>
You create a new element in the array--"date"--but then never use it. Instead, you use something called "datefield"??? What is going on?

*********

Another way to do this, though, would be to enhance your SQL query. And in the process get rid of the need to convert the date in PHP.

You don't bother to show that to us, but just as an example (and guessing about your date vs. datefield funniness):
Code:
SELECT DATE_FORMAT(date_ts,'%d/%m/%Y') AS datefield,
       datefield, shift, reason, person_covering, cost,
       IF ( staff_approved LIKE 'yes%', 'yes', 'no' ) AS doEdit
FROM yourtable
WHERE ...
My "doEdit" may be backwards. If so, just swap the 2nd and 3rd arguments of the if.

Then, in the PHP code:
Code:
<?php
while($rows=mysql_fetch_array($result))
{
?>
    <tr>
        <td nowrap><?php echo $rows['datefield']; ?></td>
        <td nowrap><?php echo $rows['shift']; ?></td>
        <td nowrap> <?php echo $rows['reason'];?></td>
        <td nowrap> <?php echo $rows['person_covering']; ?></td>
        <td nowrap> &pound;<?php echo $rows['cost']; ?></td>
        <td nowrap><?php if($rows['doEdit'] == 'yes')
        {
            ?>
            <a href="updaterecord.php?id=<?php echo $rows['id'];?>">Edit</a>
            <?php
        }
        ?>
        </td>
    </tr>
<?php
}
?>
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
jake66 (09-05-2012)
Old 09-05-2012, 08:44 PM   PM User | #4
jake66
New Coder

 
Join Date: Jul 2011
Posts: 23
Thanks: 8
Thanked 0 Times in 0 Posts
jake66 is an unknown quantity at this point
Thanks Old Pedant, as ever. To your first point - sorry that was a copy and paste mistake - different versions....long story.
Thanks so much for your query and the if which was very useful and solved my problem!
Jake
jake66 is offline   Reply With Quote
Reply

Bookmarks

Tags
array, editable, fetch, mysql, php

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:56 AM.


Advertisement
Log in to turn off these ads.