Have problem with while ($row=mysql_fecth_assoc($resultprop)) embedded in For Loop

Discussion in 'PHP' started by hamzatki, Feb 20, 2007.

  1. #1
    Hi All,

    I have problem using while() inside a For Loop in the code below.

    <?php



    $allprop_id = explode(",", $prop_id1);

    for($i = 0; $i < $selected; $i++)

    {

    $test = $allprop_id[$i];

    echo "$test <br />";

    // displaying the correct values of $test for the current values of $i

    $sqlprop = "SELECT * FROM property where prop_id ='$test'";
    $resultprop = mysql_query($sqlprop);

    while ($row=mysql_fetch_assoc($resultprop))

    {
    $email=$row["$email"]; $prop_id = $row["$prop_id"] ...........;

    $sqlfn_new = "INSERT INTO enquiry (email, prop_id, ......) VALUES('$email, '$prop_id', ......)";

    $resultfn_new = mysql_query($sqlfn_new);

    // inserting the same first values all again
    echo "$test <br />";
    // keep on displaying the same value of $test for $i = 1

    }

    echo "$test <br />";
    // display the correct values of $test for the current values of $i


    }

    ?>

    The impression I had was that the pointer was not return to the top and when I used mysql_data_seek($resultprop, 0), it dispalyed error "Offset 0is invalid for Mysql result iondex 16 (or the query data is unbuffered)".

    Please I need your help.
     
    hamzatki, Feb 20, 2007 IP
  2. DeViAnThans3

    DeViAnThans3 Peon

    Messages:
    785
    Likes Received:
    83
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Where is the $selected variable??
    You use this code:
    for($i = 0; $i < $selected; $i++)
    Code (markup):
    However, I never see $selected declared before? :) Maybe that's the problem?
    $selected could be by example:
    $selected = array_count_values($allprop_id);
    Code (markup):
    Hope this helps.

    *ps: next time better embed your PHP code with code-bbcodes ;) that makes it easier to read all :)
     
    DeViAnThans3, Feb 20, 2007 IP
  3. hamzatki

    hamzatki Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    sorry for that, the $selected was pass over from the previous page.
     
    hamzatki, Feb 20, 2007 IP
  4. DeViAnThans3

    DeViAnThans3 Peon

    Messages:
    785
    Likes Received:
    83
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Aha! Are you using PHP5, or PHP4?
    What you could try is: (not sure it will work thought ..)

    FIND:
    <?
    Code (markup):
    ADD AFTER:
    If $selected is passed over via GET method:
    $selected = $HTTP_GET_VARS['selected'];
    Code (markup):
    If $selected is passed over via POST method:
    $selected = $HTTP_POST_VARS['selected'];
    Code (markup):
    If it doesn't works, you could start debugging. By example: get the content of $selected with this line of code (new line after declaring $selected):
    die($selected);
    Code (markup):
    If it seems $selected hasn't got a numeral value, or is even empty, than the problem might be in the variable passing.. let me know :)
     
    DeViAnThans3, Feb 20, 2007 IP
  5. hamzatki

    hamzatki Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks so much for your interest, really i do not have any problem with the $selected. My main issue is the $test value inside the while{ }.

    I have a result of a query from my table being passed to a form with checkbox fields on each row, this allow the user to further select from the list of options listed by checking the checkboxes.

    The rows (this contain a unique field -- prop_id1) of the checked boxes and the total number of rows checked ($selected) are now passed to the page above.

    What i wanted to do is to use each value of prop_id1 to retrieve another set of records from another table. That is what i have shown above.

    If one checkbox is checked the code work fine.

    In a situation where 2 or more checkboxes are checked,

    $allprop_id = explode(",", $prop_id1);

    for($i = 0; $i < $selected; $i++)

    {

    $test = $allprop_id[$i];

    echo "$test <br />";

    the values of $test are correct for all values of $i.

    while ($row=mysql_fetch_assoc($resultprop))
    {
    -----------
    ------------
    echo "$test <br />";
    Inside the while {} statement the value of $test is the same for different values of $i.
    }

    And outside the while{} and before the closing bracket of the FOR LOOP

    echo "$test <br />";
    the values of $test are correct for all values of $i.

    Definitely the problem is within the while {} statement, what it is causing it is what I do not understand.
     
    hamzatki, Feb 20, 2007 IP
  6. DeViAnThans3

    DeViAnThans3 Peon

    Messages:
    785
    Likes Received:
    83
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Well, I don't know this will help, but who doesn't tries ...
    FIND:
    while ($row=mysql_fetch_assoc($resultprop))
    Code (markup):
    REPLACE WITH:
    while ($row=mysql_fetch_array($resultprop))
    Code (markup):
    Further i'm going to look closer in $prop_id1.
     
    DeViAnThans3, Feb 20, 2007 IP
  7. DeViAnThans3

    DeViAnThans3 Peon

    Messages:
    785
    Likes Received:
    83
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Could you show me the HTML code of the PREVIOUS page? (the form should be sufficient)
     
    DeViAnThans3, Feb 20, 2007 IP
  8. hamzatki

    hamzatki Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Hi Grunt,

    This is the form.

    <table border='0' width='100%'>
    <form action='search_prop1.php' method='post'>
    <tr bgcolor='e6e6e6'>
    <td valign='top' class='txt_cont_gris' width='20%'>Property Type : </td><td valign='top' class='txt_cont_gris'><font color='Maroon'><b>$property_type</b></font> &nbsp; &nbsp; for <font color='Maroon'><b>$mode
    <div align='right'>
    $bus_name &nbsp; &nbsp; &nbsp; &nbsp;
    <input type='checkbox' name='prop_id[]' size='4' value='$prop_id' class='text'>Contact Agents</div></b></font></td>
    </tr>

    <tr bgcolor='e6e6e6'>
    <td valign='top' class='txt_cont_gris' width='20%'>No. of Bedrooms : </td><td valign='top' class='txt_cont_gris'><b>$no_bedrms</b></td>
    </tr>

    <tr bgcolor='e6e6e6'>
    <td valign='top' class='txt_cont_gris' width='20%'>No. of Bathrooms : </td><td valign='top' class='txt_cont_gris'><b><b></b>$no_bathrms</b></td>
    </tr>

    <tr bgcolor='e6e6e6'>
    <td valign='top' class='txt_cont_gris' width='20%'>Area Located : </td><td valign='top' class='txt_cont_gris'><b>$area1</b></td>
    </tr>

    <tr bgcolor='e6e6e6'>
    <td valign='top' class='txt_cont_gris' width='20%'>Price Range : </td><td valign='top' class='txt_cont_gris'><font color='Maroon'><b>$price_range</b></font></td>
    </tr>

    <tr bgcolor='e6e6e6'>
    <td valign='top' class='txt_cont_gris' width='20%'>More Details : </td><td valign='top' class='txt_cont_gris'>
    $comments</td>
    </tr>

    <tr bgcolor=''>
    <td valign='top' height='5' class='txt_cont_gris' width='20%'></td><td valign='top' class='text' align='center'>
    <!-- <hr color='8d977e' width='250'> -->
    </td>
    </tr>

    </table>
    </td>

    </tr>



    ";
    }

    ?>


    <?php
    }


    if ($area1 == "")

    {echo "
    <tr bgcolor='ffffff'>
    <td valign='top' class='textf' colspan=2>
    <center><font color='red'></font><br><b> Match not Found !</b>

    <br><br><br><br>

    <object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0' width='300' height='250' id='sqr_ad' align='middle'>
    <param name='allowScriptAccess' value='sameDomain' />
    <param name='movie' value='movies/sqr_ad.swf' /><param name='quality' value='high' /><param name='bgcolor' value='#ffffff' /><embed src='movies/sqr_ad.swf' quality='high' bgcolor='#ffffff' width='300' height='250' name='sqr_ad' align='middle' allowScriptAccess='sameDomain' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' />
    </object>

    </center>
    </td>
    </tr> ";}




    }



    // end displaying properties
    ?>

    </table>
     
    hamzatki, Feb 21, 2007 IP
  9. webisfun

    webisfun Peon

    Messages:
    57
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    a shot in the dark, I'm too lazy to code it on my server to test it,

    on the top of my head I would try

    unset($resultprop);
    $sqlprop = "SELECT * FROM property where prop_id ='$test'";
    $resultprop = mysql_query($sqlprop);

    let us know if it works =)
     
    webisfun, Feb 21, 2007 IP
  10. hamzatki

    hamzatki Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Hi webisfun,

    Thanks for your attempt, but it did not work.

    Still having the same problem, it will do the first one and use the same record for the other ones.

    Well I like your name WEB IS FUN, but if one is on the other side of the fun it can be stressful.
     
    hamzatki, Feb 21, 2007 IP
  11. hamzatki

    hamzatki Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Well, I thank everyone who has one way or the other looked at this issue and I am using this opportunity to aks for more HELP from all.

    Please help!!!
     
    hamzatki, Feb 22, 2007 IP
  12. DeViAnThans3

    DeViAnThans3 Peon

    Messages:
    785
    Likes Received:
    83
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Sorry, were offline for a few days :)

    I just saw you're using $prop_id as a submitted value. Maybe we can do something with that without using the $allprop_id?

    FIND:
    $allprop_id = explode(",", $prop_id1);
    PHP:
    REPLACE WITH:
    $allprop_id = $_POST['prop_id'];
    PHP:
    Try, and let me know :)
    Chrs,
    hans

    **note: this code is not very secure atm, but it is just testing code, so ;)
     
    DeViAnThans3, Feb 23, 2007 IP
  13. webisfun

    webisfun Peon

    Messages:
    57
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #13
    are you sure you want to use :

    $prop_id = $row["$prop_id"]
    and not
    $prop_id = $row["prop_id"] ... if prop_id is a field name from your select, you shouldn't use the variable, but the string.

    still a try, I promess, if this one doesn't work, I'll take the time to test it ... for real :p :)
     
    webisfun, Feb 23, 2007 IP
  14. DeViAnThans3

    DeViAnThans3 Peon

    Messages:
    785
    Likes Received:
    83
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Haha! You're completely right .. how could I overlook that :s
    I'm quite sure this'll fix the for loop, as else it only gives one $row[] var.
     
    DeViAnThans3, Feb 23, 2007 IP