Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2011
    Posts
    23
    Thanks
    8
    Thanked 0 Times in 0 Posts

    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

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    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!||||

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,537
    Thanks
    77
    Thanked 4,381 Times in 4,346 Posts
    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.

  • Users who have thanked Old Pedant for this post:

    jake66 (09-05-2012)

  • #4
    New Coder
    Join Date
    Jul 2011
    Posts
    23
    Thanks
    8
    Thanked 0 Times in 0 Posts
    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


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •