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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

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

Post 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; ?>
 
Attachments
well were it is red rings there i want 3 diffrent tabledatas like above code... $swordmen (8), $macemen(5), $pikemen(3) however it only returns first array (swsordmen) when i need it to collect from all 3
well were it is red rings there i want 3 diffrent tabledatas like above code... $swordmen (8), $macemen(5), $pikemen(3) however it only returns first array (swsordmen) when i need it to collect from all 3
Problem.jpg (16.88 KiB) Viewed 346 times
Last edited by Goofan on Mon Nov 16, 2009 2:04 pm, edited 8 times in total.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

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

Post 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.
Last edited by McInfo on Thu Jun 17, 2010 2:39 pm, edited 2 times in total.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Post by Jonah Bron »

...And remember to use helpful subject titles.
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

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

Post 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 =)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

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

Post 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:
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

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

Post 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'];
}
?>
 
 
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

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

Post 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 =)
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

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

Post 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.
Last edited by McInfo on Thu Jun 17, 2010 2:40 pm, edited 1 time in total.
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

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

Post 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
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

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

Post by McInfo »

Instead of $swordmen, use $my['Swordmen']. Instead of $macemen, use $my['Macemen'].

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Thu Jun 17, 2010 2:41 pm, edited 1 time in total.
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

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

Post 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
Attachments
this is the database where im collecting from
this is the database where im collecting from
problem(1) database.jpg (7.25 KiB) Viewed 238 times
thanks for the help though
thanks for the help though
Problem(1).jpg (21.42 KiB) Viewed 241 times
Last edited by Goofan on Mon Nov 16, 2009 3:12 pm, edited 1 time in total.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

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

Post 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.
Last edited by McInfo on Thu Jun 17, 2010 2:41 pm, edited 1 time in total.
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

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

Post 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?
Last edited by Goofan on Mon Nov 16, 2009 3:12 pm, edited 1 time in total.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

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

Post 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.
Last edited by McInfo on Thu Jun 17, 2010 2:42 pm, edited 1 time in total.
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

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

Post by Goofan »

all this posted within the while or above? or how?

no i didnt get it...
Post Reply