Jump to content

help simplify some code from database


petewall
Go to solution Solved by petewall,

Recommended Posts

hey, i have a little problem, i'm fetching data from a table with 20x3 columns. i've namned them 

j1 -> j20,

k1 -> k20,

z1 -> z20.

i need to check wether j1 -> j20 is empty and that k1 -> k20 is numeric.

and if both these criteria are not met, none of j, k or z should be outputted.

i've tried with 


if(!empty($row['j1']) && is_numeric($row['k1'])) {

echo $row['z1']." ";

echo $row['k1']." ";

echo $row['j1'];

echo "<br />";

}


if(!empty($row['j2']) && is_numeric($row['k2'])) {

echo $row['z2']." ";

echo $row['k2']." ";

echo $row['j2'];

echo "<br />";

}



it works, but theres just ALOT of code for something small and i'm wondering if there's another way to do this, maybe with a for-loop or something?

 

i tried with:

for ($j = 1; $j <= 20; $j++) {

  if(!empty($row['j1$j'])) {

  echo $row['z$j']." ";

  echo $row['k$j']." ";

  echo $row['j$j'];

  echo "<br />";

 

  }

But i'm guessing you can't have the $j inside the $row[''].

Anyone got any ideas? on how to make this piece of code more simple?

Link to comment
Share on other sites

the solution to simplifying the logic needed to manipulate the data will be to correct the design of your database table.

 

database tables should be designed to store data using one row per related data set. just guessing but you likely need three columns (whatever the corresponding/related j, k, and z values are), with 20 rows (or whatever number of actual data sets there are.)

Link to comment
Share on other sites

  • Solution

I actually solved it with

for ($j = 1; $j <= 20; $j++) {
  if(!empty($row['j'.$j]) && is_numeric($row['k'.$j])) {
  echo $row['z'.$j]." ";
  echo $row['k'.$j]." ";
  echo $row['j'.$j];
  echo "<br />";
 
  }
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.