Solved

putting array into array

Posted on 2009-07-08
14
223 Views
Last Modified: 2013-12-13
I have array of this

        $l[] = "<tr><td>".mssql_result($resulta, $ia, 'location')."</td>
            <td>".mssql_result($resulta, $ia, 'MC9063')."</td>
            <td>".mssql_result($resulta, $ia, 'MC9090')."</td>
            <td>".mssql_result($resulta, $ia, 'MC9060')."</td>
            <td>".mssql_result($resulta, $ia, 'MC3090')."</td>
            <td>".mssql_result($resulta, $ia, 'MC9060G')."</td></tr>";

I have another array of this
        $b[] = mssql_result($result, $i, 'Description');

Which would output -> MC9063MC9090MC9060MC3090MC9060G


I need to make my array $l more dynamic and less static becuase the Description will very

I made a thrid aray bellow
        $c[] = "<td>.mssql_result($"."resulta, $"."ia, '".mssql_result($result, $i, 'Description')."').</td>";

my goal is this

        $l[] = "<tr><td>".mssql_result($resulta, $ia, 'location')."</td>". implode($c) .   " </tr>";


The Problem I am having is the output in my table is this
locationabc
.mssql_result($resulta, $ia, 'MC9063').
.mssql_result($resulta, $ia, 'MC9090').
.mssql_result($resulta, $ia, 'MC9060').
etc

It is putting text instead of doing the php


I can not get around this.
I am hoping you understand what I am trying to do.
0
Comment
Question by:adamn123
  • 8
  • 6
14 Comments
 
LVL 1

Expert Comment

by:deefjuh
ID: 24804058
This must be some complex table you are trying to create.

After reading it thoroughly I came with a (not so elegant) solution.

The third array you describe:

"<td>.mssql_result($"."resulta, $"."ia, '".mssql_result($result, $i, 'Description')."').</td>";
Is in fact a literal: the right mssql_result is outside the "" after the dot, so its's concatenated.

So my guess is:
You tried to construct a new function call with all the parameters dynamically.
You used the $"."ia construction because otherwise it got evaluated (it probably gave errors, but because you made it a string with " instead of ' it will evaluate the variable).

Aren't you trying to achieve:
            "<td>".mssql_result($resulta, $ia, mssql_result($result, $i, 'Description'))."</td>";

If it is not the solution ( I have no clue what you are trying to achieve) a few tips:
"" --> for string where variables will be printen
'' --> nothing willl be evaluated
count ( and ) : functionscalls need to be closed always

Let me know

0
 

Author Comment

by:adamn123
ID: 24804174
Belllow is the full code.

Maybe it will help make more sense
$query = "select Distinct m.ID, m.Description

from conf_Inventory_Model m

      join Inventory i

        on m.ID=i.ID_Model

      join conf_Company c

        on i.ID_Company=c.ID

where c.Company='$usernew' and

      substring(m.Description, 1, 2)= 'MC'";
 
 

$result = mssql_query($query);
 

$a = array();

$b = array();

$c = array();

$l = array();
 

mt_srand((double)microtime()*1000000);

for($i = 0; $i < mssql_num_rows($result); $i++)

{
 

        $a[] = "Sum(Case when i.ID_Model=".mssql_result($result, $i, 'id')." then 1 else 0 end) as '". mssql_result($result, $i, 'Description') ."'\n";

        $b[] = "<td>".mssql_result($result, $i, 'Description')."</td>";

        $c[] = "<td>.mssql_result($"."resulta, $"."ia, '".mssql_result($result, $i, 'Description')."').</td>";

}
 
 

$querya = "SELECT l.Location , " . implode(", ", $a) .

       " FROM Inventory I

         JOIN conf_Company C

         ON I.ID_Company=c.ID

         JOIN conf_Location l

         ON I.ID_Location=L.ID

         JOIN conf_Inventory_Model M

         ON I.ID_Model=M.ID

         WHERE C.Company='$usernew'

         GROUP BY L.Location";
 
 
 
 

$resulta = mssql_query($querya);
 

for($ia = 0; $ia < mssql_num_rows($resulta); $ia++)

{

        $l[] = "<tr><td>".mssql_result($resulta, $ia, 'location')."</td>". implode($c) .   " </tr>";
 
 
 

}

ho '<br><br>';

echo $result;
 

echo '<br><br>';

echo '<Table>';

echo '<tr>';

echo '<td>Location</td>';

	echo implode($b);

echo '</tr>';

echo '<tr>';

	echo implode($l);

echo '</tr>';

echo '</Table>';

Open in new window

0
 

Author Comment

by:adamn123
ID: 24804751
line 51
needs ec added don't how I missed that

echo '<br><br>';
0
 

Author Comment

by:adamn123
ID: 24805245
Any ideas?
I have also increased the points
0
 
LVL 1

Expert Comment

by:deefjuh
ID: 24805516
A couple of questions:

Why is $usernew never set?
What are you doing with the generated number?

Extra protip: use self-describing variables!!
It makes it much more readible.
Anyways try running the code below (and read my comments!!!)



<?php

//DS: Where is $usernew set?

$query = "select Distinct m.ID, m.Description

from conf_Inventory_Model m

      join Inventory i

        on m.ID=i.ID_Model

      join conf_Company c

        on i.ID_Company=c.ID

where c.Company='$usernew' and

      substring(m.Description, 1, 2)= 'MC'"; 

 

 

$result = mssql_query($query);

 

$a = array();

$b = array();

$c = array();

$l = array();

 

mt_srand((double)microtime()*1000000); //DS: not needed?

for($i = 0; $i < mssql_num_rows($result); $i++)

{

 

        $a[] = "Sum(Case when i.ID_Model=".mssql_result($result, $i, 'id')." then 1 else 0 end) as '". mssql_result($result, $i, 'Description') ."'\n";

        $b[] = "<td>".mssql_result($result, $i, 'Description')."</td>"; 

  /*

  DS:

  Where is $ia set? or do you mean the values of $a?

  then it should be: $a[$i]

  */

        //$c[] = "<td>".mssql_result($resulta, $ia, mssql_result($result, $i, 'Description'))."</td>";

  $c[] = "<td>".mssql_result($resulta, $a[$i], mssql_result($result, $i, 'Description'))."</td>"; //DS: you mean something like this?

  

}

 

 

$querya = "SELECT l.Location , " . implode(", ", $a) .

       " FROM Inventory I

         JOIN conf_Company C

         ON I.ID_Company=c.ID

         JOIN conf_Location l

         ON I.ID_Location=L.ID

         JOIN conf_Inventory_Model M

         ON I.ID_Model=M.ID

         WHERE C.Company='$usernew'

         GROUP BY L.Location";

 

 

 

 

$resulta = mssql_query($querya);

 

for($ia = 0; $ia < mssql_num_rows($resulta); $ia++)

{

        $l[] = "<tr><td>".mssql_result($resulta, $ia, 'location')."</td>". implode($c) .   " </tr>";

 

 

 

}

echo '<br><br>';

echo $result;

 

echo '<br><br>';

echo '<Table>';

echo '<tr>';

echo '<td>Location</td>';

 echo implode($b);

echo '</tr>';

echo '<tr>';

 echo implode($l);

echo '</tr>';

echo '</Table>';

?>

Open in new window

0
 

Author Comment

by:adamn123
ID: 24805754
new user is set above it is the login

ia -> this where set in the for and thats what cousing my issue.

it is set below
0
 
LVL 1

Expert Comment

by:deefjuh
ID: 24805966
$ia is indeed set at the bottom... but you are using it above this!

I can safely assure you that the first time $ia is called for, it's not there: PHP works from top -> bottom
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:adamn123
ID: 24805987
I know but I can not figure out how to make this code work
no matter how I arrage it I can not get it to work
0
 

Author Comment

by:adamn123
ID: 24806418
I have attached another code to try and get this resolved.

All I need to do is make this part automatic
            <td>".mssql_result($resulta, $ia, $c[0])."</td>
            <td>".mssql_result($resulta, $ia, $c[1])."</td>
            <td>".mssql_result($resulta, $ia, $c[2])."</td>
            <td>".mssql_result($resulta, $ia, $c[3])."</td>
            <td>".mssql_result($resulta, $ia, $c[4])."</td></tr>";

With some while statment or something

$query = "select Distinct m.ID, m.Description

from conf_Inventory_Model m

      join Inventory i

        on m.ID=i.ID_Model

      join conf_Company c

        on i.ID_Company=c.ID

where c.Company='$usernew' and

      substring(m.Description, 1, 2)= 'MC'";

 

 

$result = mssql_query($query);

 

$a = array();

$b = array();

$c = array();

$l = array();
 

mt_srand((double)microtime()*1000000);

for($i = 0; $i < mssql_num_rows($result); $i++)

{

 

        $a[] = "Sum(Case when i.ID_Model=".mssql_result($result, $i, 'id')." then 1 else 0 end) as '". mssql_result($result, $i, 'Description') ."'\n";

        $b[] = "<td>".mssql_result($result, $i, 'Description')."</td>";

        $c[] = mssql_result($result, $i, 'Description');
 

}

 

 

$querya = "SELECT l.Location , " . implode(", ", $a) .

       " FROM Inventory I

         JOIN conf_Company C

         ON I.ID_Company=c.ID

         JOIN conf_Location l

         ON I.ID_Location=L.ID

         JOIN conf_Inventory_Model M

         ON I.ID_Model=M.ID

         WHERE C.Company='$usernew'

         GROUP BY L.Location";

 

 

 

 

$resulta = mssql_query($querya);
 

 

for($ia = 0; $ia < mssql_num_rows($resulta); $ia++)

{

        $l[] = "<tr><td>".mssql_result($resulta, $ia, 'location')."</td>

		<td>".mssql_result($resulta, $ia, $c[0])."</td>

		<td>".mssql_result($resulta, $ia, $c[1])."</td>

		<td>".mssql_result($resulta, $ia, $c[2])."</td>

		<td>".mssql_result($resulta, $ia, $c[3])."</td>

		<td>".mssql_result($resulta, $ia, $c[4])."</td></tr>";
 
 

}
 
 
 

echo '<br><br>';

echo $i;

echo '<br><br>';

echo '<Table>';

echo '<tr>';

echo '<td>Location</td>';

	echo implode($b);

echo '</tr>';

echo '<tr>';

	echo implode($l);

echo '</tr>';

echo '</Table>';

Open in new window

0
 
LVL 1

Accepted Solution

by:
deefjuh earned 250 total points
ID: 24806653
You would do that with a foreach-loop (like I attached).

You use an array (by setting it to $l[] --> the brackets cast it to an array), and implode it afterwards (wihout actually using it as an array).
When you want to set it as a string, just use the . as glue to get the pieces of text together.


replace
echo implode($l);

with
echo $l;
$l = '<tr>';

foreach($c as $key => $value){

    $l .= "<td>".mssql_result($resulta, $ia, $value)."</td>";

}

$l = '</tr>';
 

/*

replace below

echo implode($l);
 

with

echo $l;

*/

Open in new window

0
 
LVL 1

Expert Comment

by:deefjuh
ID: 24806704
You use an array (by setting it to $l[] --> the brackets cast it to an array), and implode it afterwards (wihout actually using it as an array).
When you want to set it as a string, just use the . as glue to get the pieces of text together.

By this I mean:
Assign variables without a []. (look below).
 
www.php.net :love it, embrace it, use it! And look at the usercomments below the function explanation, they contain usefull snippets.
 
 

$string = 'my text'; 

$string .= ' is now bigger';
 

echo $string; // will print 'my text is now bigger'
 

while you use:

$string[] = 'my text';

$string[] = ' is now bigger';

echo implode($string);
 

This is not an error, but a bad coding practice.

Open in new window

0
 

Author Comment

by:adamn123
ID: 24807151
no matter what I do I get

Warning: mssql_result() [function.mssql-result]: Bad row offset (139)
0
 
LVL 1

Expert Comment

by:deefjuh
ID: 24807748
I see I made an error at my foreach-loop. I misinterpreted the wanted result for the rows.
Anyways, if you gave some test data (the create query for the data and some rows), I would get it up and running in notime.
0
 

Author Comment

by:adamn123
ID: 24807765
$query = "select Distinct m.ID, m.Description
from conf_Inventory_Model m
      join Inventory i
        on m.ID=i.ID_Model
      join conf_Company c
        on i.ID_Company=c.ID
where c.Company='$usernew' and
      substring(m.Description, 1, 2)= 'MC'";
 
 
$result = mssql_query($query);
 
$a = array();
$b = array();
$c = array();


mt_srand((double)microtime()*1000000);
for($i = 0; $i < mssql_num_rows($result); $i++)
{
 
        $a[] = "Sum(Case when i.ID_Model=".mssql_result($result, $i, 'id')." then 1 else 0 end) as '". mssql_result($result, $i, 'Description') ."'\n";
        $b[] = "<td>".mssql_result($result, $i, 'Description')."</td>";
        $c[] =  mssql_result($result, $i, 'Description');

}
 
$querya = "SELECT l.Location , " . implode(", ", $a) .
       " FROM Inventory I
         JOIN conf_Company C
         ON I.ID_Company=c.ID
         JOIN conf_Location l
         ON I.ID_Location=L.ID
         JOIN conf_Inventory_Model M
         ON I.ID_Model=M.ID
         WHERE C.Company='$usernew'
         GROUP BY L.Location";
 
$resulta = mssql_query($querya);
$ia = mssql_num_rows($resulta);


$ia = $ia -1;
$counter = 0;
$m="";

while ( $counter <= $ia ) {

$m .= '<tr>';
$m .= '<td>'.mssql_result($resulta, $counter, 'location').'</td>';
foreach($c as $key){
    $m .= "<td>".mssql_result($resulta, $counter, $key)."</td>";
}
$m .= '</tr>';
$counter = $counter +1;
}
echo '<br><br>';
echo '<Table>';
echo '<tr>';
echo '<td>Location</td>';
echo implode($b);
echo '</tr>';
echo  $m;
echo '</Table>';

//close the connection
mssql_close($dbhandle);
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Post form to two locations 1 30
phpmyadmin 3 31
form button worked now it doesnt anymore 9 36
phpmailer in WHILE loop - weird results 10 27
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now