Page 1 of 2

Need a loop that gets from diffrent datacells or a solution!

Posted: Fri Nov 13, 2009 2:36 pm
by Goofan
this is my current problem
i need to get out 3 diffrent rows from the datarows while its in a WHILE...
i got the while now i need to tell it to place "first" at datatable 1 and "second" at datatable 2 and "third" at datatable 3 and so on

Code: Select all

 
 <table border="1">
   <tr>
    <th>Units</th>
      <th>AtkArm</th>
    <th>Buyfor</th>
      <th>Sellfor</th>
    <th>Swordmen</th>
   </tr>
<!-- starting the while -->
 <?php while ($row1 = mysql_fetch_assoc($result1)): ?>
 <tr>
      <th><?php echo $row1['Units'] ;?></th>
    <th><?php echo $row1['AtkHp'] ;?></th>
      <th><?php echo $row1['Buyfor'] ;?></th>
    <th><?php echo $row1['Sellfor'] ;?></th>
 
 <!-- need to get out three diffrent tabledatas... but on the same row so to say -->
 
      <th><?php echo $swordmen ?></th> <--------------Here i got the problem
 
 
<!--it should have theses too-->
 <!-- <th><?php echo $macemen?></th>   --> <!--- picture will show ---> 
<!-- <th><?php echo $pikemen?></th>   -->
 
 
    <th>
        <font color="black" valign= "top"><input style='width:45;height:20' type="text" name="input ITD1" size="250" class="textbox" value=""></font>
        <input style='width:45;height:30;font-weight:bold' name="submit" type="submit" class="submit" value="Buy!">
    </th>
   </tr>
<!-- ending the while -->
 <?php endwhile; ?>
 

Re: Hi i got this problem... can someone help me?

Posted: Fri Nov 13, 2009 5:47 pm
by McInfo
Please use [code=php][/code] or [code=html][/code].

What final HTML are you trying to generate?

Edit: This post was recovered from search engine cache.

Posted: Fri Nov 13, 2009 6:34 pm
by Jonah Bron
...And remember to use helpful subject titles.

Re: Hi i got this problem... can someone help me?

Posted: Sat Nov 14, 2009 2:34 pm
by Goofan
i think i need some kind of loop. that can tell it to take first 1 then the second one and so on from the database


only problem is i dont know how to code it as im not that good at coding for that:





-thanks for any help =)

Re: Hi i got this problem... can someone help me?

Posted: Sat Nov 14, 2009 6:28 pm
by John Cartwright
This can probably be handled by modifying your existing query. Post the SQL, and please, change your subject to something more descriptive :evil:

Re: Need a loop that gets from diffrent datacells =)

Posted: Sun Nov 15, 2009 2:54 pm
by Goofan
My sql´s =) sorry for not changing the title!

Code: Select all

 
    <?php
    // Get the database connector stuff
    include "../login/database.php";
     
    // Build our query
    $sql1 = 'SELECT * FROM `infantries`';
     
    // Get the result if there is one
   // DO NOT die() IN PRODUCTION!!!
   if (!$result1 = mysql_query($sql1)) 
    {
     die('The query<br /><strong>' . $sql1 . '</strong><br />failed:<br />' . mysql_error());
    }
 ?>
 
 
 
<?php
 
 
  $id =(isset($_GET['saved_id'])) ? (int)$_GET['saved_id'] : false;
    if($id !== false) {
    $sql="SELECT * FROM konto WHERE saved_id=$id";      //selecting all from DB "Konto" where saved_id is the same as in the array $id
 } 
    else 
 {
    echo "NO saved_id!";
 }
     
 
$result = mysql_query($sql) or die(mysql_error());//Välj all info i tall. //hämtar all info från tabell
 
                 
while($row = mysql_fetch_array( $result )) //hämtar info från tabell.
{
$swordmen=$row['swordmen'];
$macemen=$row['macemen'];
$pikemen=$row['pikemen'];
}
?>
 
 

Re: Need a loop that gets from diffrent datacells or a solution!

Posted: Mon Nov 16, 2009 1:38 pm
by Goofan
is there any solution to the problem im having? dont need to be a loop just making it so that i works like i need it to =)

Re: Need a loop that gets from diffrent datacells or a solut

Posted: Mon Nov 16, 2009 1:59 pm
by McInfo
Put the owned units in an associative array so they are accessible like

Code: Select all

$my['Swordmen'] // Returns 8
Then you can get the number associated with the given unit with

Code: Select all

$row1['Units'] // Returns 'Swordmen'
$my[$row1['Units']] // Returns 8
Remember that strings are case-sensitive.

Edit: This post was recovered from search engine cache.

Re: Need a loop that gets from diffrent datacells or a solution!

Posted: Mon Nov 16, 2009 2:07 pm
by Goofan
the problem isnt that i need to access the (8)... i need to collect from swordmen (which containes the 8 ) then i got some others.... they got other value (like (5) and (7) and so on however as i put the array within a while i only get swordmens value got any idé on how to solve this problem? (for more info check the comment on the picture)

(tell me if i missunderstood ure answer and if so plz explain ure part abit more)


thanks for all the help so far

Re: Need a loop that gets from diffrent datacells or a solut

Posted: Mon Nov 16, 2009 2:21 pm
by McInfo
Instead of $swordmen, use $my['Swordmen']. Instead of $macemen, use $my['Macemen'].

Edit: This post was recovered from search engine cache.

Re: Need a loop that gets from diffrent datacells or a solution!

Posted: Mon Nov 16, 2009 2:32 pm
by Goofan
well i get that but i dont see how that can fix the problem...
the picture im sending in might explain better then words!


Either i dont get ure solution or ure solution aint the one im in need of(thanks anyway)

Code: Select all

 
 <table border="1">
   <tr>
    <th>Units</th>
        <th>AtkArm</th>
    <th>Buyfor</th>
        <th>Sellfor</th>
    <th>You Own</th>
        <th>Buy</th>
   </tr>
 <?php while ($row1 = mysql_fetch_assoc($result1)): ?>
 <tr>
        <th><?php echo $row1['Units'] ;?></th>
    <th><?php echo $row1['AtkHp'] ;?></th>
        <th><?php echo $row1['Buyfor'] ;?></th>
    <th><?php echo $row1['Sellfor'] ;?></th>
        <th><?php echo $my['swordmen'] ?></th> <--- this needs to be 3 diffrent datatables (swordmen, macemen, pikemen---|
    <th>
        <font color="black" valign= "top"><input style='width:45;height:20' type="text" name="input ITD1" size="250" class="textbox" value=""></font>
        <input style='width:45;height:30;font-weight:bold' name="submit" type="submit" class="submit" value="Buy!">
    </th>
   </tr>
 <?php endwhile; ?>
 </table>
 
 
as u see i dont think ure solution makes any diffrent besides giving me 3 new rows

Re: Need a loop that gets from diffrent datacells or a solut

Posted: Mon Nov 16, 2009 2:45 pm
by McInfo
I'll say it again.

Code: Select all

<?php echo $my[$row1['Units']]; ?>
Somewhere before the loop, you need to assign values to $my['Swordmen'], $my['Macemen'], and $my['Pikemen'].

Edit: This post was recovered from search engine cache.

Re: Need a loop that gets from diffrent datacells or a solution!

Posted: Mon Nov 16, 2009 2:52 pm
by Goofan
i guess ure saying something like this?

Code: Select all

 
    $my=$row['swordmen'];
    $my=$row['macemen'];
    $my=$row['pikemen'];
 
and then within the while this?

Code: Select all

 
<?php echo $my[$row1['Units']]; ?>
 
is this correct or can u put ure code into mine to show?

Re: Need a loop that gets from diffrent datacells or a solut

Posted: Mon Nov 16, 2009 3:04 pm
by McInfo
Almost.

Code: Select all

$my['Swordmen'] = $row['swordmen'];
$my['Macemen'] = $row['macemen'];
$my['Pikemen'] = $row['pikemen'];
Please use or when posting PHP code.

Edit: This post was recovered from search engine cache.

Re: Need a loop that gets from diffrent datacells or a solution!

Posted: Mon Nov 16, 2009 3:10 pm
by Goofan
all this posted within the while or above? or how?

no i didnt get it...