Hi all,

I have code as follows, to fetch rows from mysql table. Table it fetches is given below. I want to access now each cell in the table, how do I do that? For e.g, I want to multiply Rank for the Job "transform Brand image by 82% and put it in 4th column to that table.

$sql="SELECT * FROM peoplegrid WHERE Employee ='$name'"; //'".$q."'";

$result = mysql_query($sql);
$row= mysql_fetch_array($result);

//here is where I have tried to access the elements of array
echo $row[0]." - ".$row[3];
echo "<br>";
echo $row[2];
//this is orginal while loop

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Function</th>
<th>City</th>
<th>Rank</th>
<th>Job</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row[0] . "</td>";
  echo "<td>" . $row[1] . "</td>";
  echo "<td>" . $row[2] . "</td>";
  echo "<td>" . $row[3] . "</td>";
  echo "<td>" . $row[4] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?> 

To access fields of a row use associative index that mysql_fetch_array function returns:

echo $row['Firstname']." - ".$row['Rank'];

To address particular rows (using SELECT, UPDATE or DELETE queries) you have to use WHERE condition(s) that return just one row. In your case it would be a good thing to have another unique field for each row (e.g. id) which would be also a primary key. It enables you to address particular row and also improves performance.

Hi,

Thanks for clarifying this. It really helped though did not provide me the straight answer. I am looking for ways to access it by row number and column number like in excel you do.

You can store the rows in question in an array (could impact performance if the fieldset is large).

$rows_array = array();
// index for row numbers
$row_no = 0;

// firs store all data in a multidimensional array
while($row = mysql_fetch_array($result))
{
    $rows_array[row_no] =$row;
    row_no++;
}

...

// display the table rows
foreach(rows_array as $row) {

    echo "<tr>";
    echo "<td>" . $row['Firstname'] . "</td>";
    echo "<td>" . $row['Function'] . "</td>";
    echo "<td>" . $row['City'] . "</td>";
    echo "<td>" . $row['Rank'] . "</td>";
    ...
    echo "</tr>";
}

...

// address the cell (Rank in row 4)

$myRank = rows_array[3]['Rank'];

But as I said it would be preferrable if you had another column for row numbers in your database table which would be the primary key and would help you updating particular fields. That field would replace the $row_no variable.

Just a note: spreadsheet table and database table are not exactly the same though the concept is similar.

Hi there!

I copied the code and pasted. It looks like there is small error in the code you gave.
It might just be the answer I was looking for.
Could you please confirm the code? Thanks a lot for the concept..

my full-code is as follows; and it said parse error on line 26 ??
.............

<?php
//$qg=$_GET["q"];

$name="Ramana Sethi";
$con = mysql_connect('localhost', 'root', '');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("testdb", $con);

$sql="SELECT * FROM peoplegrid WHERE Employee ='$name'"; //'".$q."'";

$result = mysql_query($sql);

//provided by Daniweb.Bojoe Recko

    $rows_array = array();
    // index for row numbers
    $row_no = 0;
    // firs store all data in a multidimensional array
    while($row = mysql_fetch_array($result))
    {
    $rows_array[row_no] =$row;
    row_no++;
    }
    ...
    // display the table rows
    foreach(rows_array as $row) {
    echo "<tr>";
    echo "<td>" . $row['Firstname'] . "</td>";
    echo "<td>" . $row['Function'] . "</td>";
    echo "<td>" . $row['City'] . "</td>";
    echo "<td>" . $row['Rank'] . "</td>";
    ...
    echo "</tr>";
    }
    ...
echo "</table>";

    // address the cell (Rank in row 4)

 $myRank = rows_array[3]['Rank'];

echo $myRank;


mysql_close($con);

?> 

Yes there are errors:

  1. remove all ... from the code. They are there for your code to insert if needed.
  2. On line 25 the $ is missing (my typo)
  3. On line 26 the $ is missing (my typo)
  4. On line 44 the $ is missing (my typo)

Sory for the typos. Here is the correct code (I hope):

<?php
//$qg=$_GET["q"];
$name="Ramana Sethi";
$con = mysql_connect('localhost', 'root', '');
if (!$con)
{
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("testdb", $con);
$sql="SELECT * FROM peoplegrid WHERE Employee ='$name'"; //'".$q."'";
$result = mysql_query($sql);
//provided by Daniweb.Bojoe Recko
$rows_array = array();
// index for row numbers
$row_no = 0;
// firs store all data in a multidimensional array
while($row = mysql_fetch_array($result))
{
    $rows_array[$row_no] =$row;
    $row_no++;
}

// display the table rows
foreach(rows_array as $row) {
    echo "<tr>";
    echo "<td>" . $row['Firstname'] . "</td>";
    echo "<td>" . $row['Function'] . "</td>";
    echo "<td>" . $row['City'] . "</td>";
    echo "<td>" . $row['Rank'] . "</td>";
    echo "</tr>";
}

echo "</table>";
// address the cell (Rank in row 4 if exist)
$myRank = $rows_array[3]['Rank'];
echo $myRank;
mysql_close($con);
?> 

Hi

Thanks indeed! That was absolutely brilliant!!
just don't know how to thank...

on a really lighter side..
One more $ was missing on line 24 foreach($rows_arry ....

It is OK. And typos happen all the time :-).

If the problem is solved please mark the thread as solved. Happy coding.

Hi

Good Morning!

Sure I just marked it solved ..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.