Forum Moderators: coopster

Message Too Old, No Replies

Help with if/else

         

dkin

2:13 am on Jun 24, 2004 (gmt 0)

10+ Year Member



I would like to have an if else statement but I am not completely sure how to do it with so many variables.

I would like if $row is empty $var is not displayed, but if $row has a value display $var, this must be done with 18 different variables.

Any Ideas?

ncreegan

2:31 am on Jun 24, 2004 (gmt 0)

10+ Year Member



It sounds like you might be able to handle this with a while loop. I can show you what to do if you go into a little more detail... I assume you're grabbing info from a database?

dkin

2:58 am on Jun 24, 2004 (gmt 0)

10+ Year Member



yes I am grabbing roughly 18 rows. each needs a prefix which is not in the db, so I am adding it and putting it into a variable, but if there is no value in the db row I do not want the variable to show at all.

ncreegan

3:22 am on Jun 24, 2004 (gmt 0)

10+ Year Member



would something like this help you?

$query="YOUR QUERY";
$result=mysql_query($query);
while($row = mysql_fetch_array($result)) {
if($row[var]) {
echo "prefix $row[var]";
}
}

I'm still not sure of the details of your query so it's hard to give you a specific solution.

dkin

3:39 am on Jun 24, 2004 (gmt 0)

10+ Year Member



but I need it with 18 different rows and prefixes.

say a row name is "AC" and I want the prefix to be AC, another row was "HpMax" and I wanted the prefix to be Hp.

What would the code look like?

dkin

4:15 am on Jun 24, 2004 (gmt 0)

10+ Year Member



I need to do this with 18 different rows, but I have 46 rows in my db, so I need to isolate the script to those specific rows.

WhosAWhata

4:20 am on Jun 24, 2004 (gmt 0)

10+ Year Member



for you prefix assuming $name is the row name (i haven't looked into mySQL yet...shh don't tell anyone) and you want the prefix to be the first two digits of the row name

$prefix = substr($row,0,2);

dkin

4:22 am on Jun 24, 2004 (gmt 0)

10+ Year Member



now I would like to define the prefix. something like this.

$AC = "AC" . row["AC"] . " ";

HelenDev

3:19 pm on Jun 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




$query="YOUR QUERY";
$result=mysql_query($query);
while($row = mysql_fetch_array($result)) {
if($row[var]) {
print($row[name].$row[var]);
}
}

dkin

3:34 pm on Jun 24, 2004 (gmt 0)

10+ Year Member



but I only want to do this with about half of my db. Is there anyway that I can restrict the script to certain rows?

Warboss Alex

3:35 pm on Jun 24, 2004 (gmt 0)

10+ Year Member



..

Try this within the while loop ..

//gets the prefix for the row in question
$pre = substr($row['name'], 0, 2);

//add all the 18 prefixes you'd want displayed here
$pre_arr = array('HP', 'AC');

//if $pre is not empty and is in the array of required prefixes, print or whatever else you want to do..
if (!empty($pre) && in_array($pre_arr, $pre) )
print $row['name'];

dkin

3:41 pm on Jun 24, 2004 (gmt 0)

10+ Year Member



I have no idea what that will do. Will it take a specified prefix and add it to the value from the db row? like this.

Prefix: Value

ergophobe

3:56 pm on Jun 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You have 18 rows. Unless each row has a thousand columns, you can probably do this by hand in five minutes. It will be hard to program it in that time.

Tom

dkin

4:24 pm on Jun 24, 2004 (gmt 0)

10+ Year Member



Im sorry I have 18 columns I would like in this script and 44 in my db, I will have thousands of rows.

Im sorry I am new to this and have been using the wrong terminology.

Warboss Alex

4:39 pm on Jun 24, 2004 (gmt 0)

10+ Year Member



So you want the prefix from the COLUMN of the data? ..

brucec

4:44 pm on Jun 24, 2004 (gmt 0)

10+ Year Member



I don't know if anybody else suggested this, but arrays work well for this...

brucec

4:44 pm on Jun 24, 2004 (gmt 0)

10+ Year Member



Oh, HelenDev suggested this in her code. Cool!

dkin

4:55 pm on Jun 24, 2004 (gmt 0)

10+ Year Member



I would like to define the prefix if possible.

ergophobe

5:19 pm on Jun 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What criteria determine whether or not a column needs a prefix. Unless I'm mistaken, you want to do the following.

- Out of 46 cols, you will always be testing the same 18 cols
- Any time those cols have values other than null or a zero-length string, you want to add a prefix. In other words, you merely want to avoid creating a value (= to the prefix) where currently there is none.

If that's the case, you merely have to test each value. To save me some writing, lets just assume that you have five cols (col1-col5) and want to add prefixes to two (col2, col4) and col1 is your primary key. You do this

$prefix_array ('col2' => 'prefix2', 'col4' => 'prefix4');

$query = "SELECT col1, col2, col4 FROM table1";
$result = mysql_query($query);

while ($row = mysql_fetch_assoc($result))
{
$update_query = "";

foreach ($row as $key => $val) // walk through our array of columns
{
if ($val) // only update if it has a value
{
$update_query .= $key . "='" . $prefix_array[$key] . $val . "',";
}
} // close foreach loop

if ($update_query) // if!$update_query, that means all cols were blank. No update
{
$update_query = substr($update_query, 0, -1); // trim off the trailing comma
$update_query = "UPDATE table1 SET " . $update_query; // note space between SET and "
mysql_query($update_query);
} // close update if

} close while loop

This of course does no error checking or anything, but that should pretty much do it for you.

dkin

5:24 pm on Jun 24, 2004 (gmt 0)

10+ Year Member



and what would I type in the print function?
to show the results I mean.

[edited by: jatar_k at 6:23 pm (utc) on June 26, 2004]

ergophobe

6:30 pm on Jun 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You would probably want to do another query after all the updating is done and output the results of that query.

Personally, I would use the mysql client interface of PhpMyAdmin or Mysql-Front or something like that to check out the results.

Tom

dkin

6:53 pm on Jun 24, 2004 (gmt 0)

10+ Year Member



I do not know how to do that.

All I wanted to do was declare the variables myself and use an if else state ment saying if the row is empty dont show the variable, can I do anthing easy like that?

ergophobe

8:01 pm on Jun 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I misunderstood, I thought you were trying to update the underlying data.

In any case, the script I gave you does most of what you want, but needs two modifications.

1. Instead of getting the data and creating an INSERT, you need to get the data and put it in a table. So instead of $update_query .= SQL statement you just do

$output_row = '<tr>';

....

$output_row .= '<td>' . $prefix_array[$key] . $val . '<td>';

.....
echo output_row . '</tr>';

2. You need to check whether this is a column that needs a prefix. Remember, the ones to which we want to add prefixes are called col2 and col4.

$prefix_cols = array('col2', 'col4');

Now we change our query to

SELECT * FROM table1

Now, inside the foreach statement, first we test that the current field is in our list of cols to be prefixed, then we make sure it has a value, all in one quick if statement:

if (in_array($key, $prefix_cols) && $val)
{
$val = $prefix_array[$key] . $val;
}

Now we put the value in a cell of our table row.
$output_row .= "<td>$val</td>"

dkin

11:26 pm on Jun 24, 2004 (gmt 0)

10+ Year Member



Im sorry, but could you paste the full script I tried doing it and am at a loss for words, I have no idea what goes where.

Thank you very much.

Sorry I am so hopeless.

jatar_k

7:21 pm on Jun 26, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Well, no one is hopeless

I think your answer is among all of the great advice you have gotten here so far but let's see if we can't help you to understand it all a little better.

I think part of the problem here is that we don't quite understand the why of what you are doing. The one drawback of a forum environment is the figuring out of the actual situation often takes longer than if you were sitting right across from each other.

So, let's go back to the problem itself

What is your database structure?
What is it exactly you are trying to do?
do you just want to display data from your table or are you trying to actually do something else with it?

The thing that is difficult to understand is why you need the prefixes just to display data.

dkin

8:01 pm on Jun 26, 2004 (gmt 0)

10+ Year Member



My database structureis, I have about 44 columns. Item_Name, Item_Level, Item_Slot etc etc. There are 18 within these 44 that I would like to be pulled with this script. AC, PR, AR, Hp, FR etc etc are all attributes of the item that I would be showing. Instead of defining a certain area for each attribute int the HTML table I am having one column dedicated to "Attributes".

Now for these 18 columns in the db if I do not have a prefix they will return like this

125, 352, 12, 452(commas for example purposes only)

No one will be able to figure out which number goes to which attribute.

So this is what I would like it to look like.

AR 125, AC 352, PR 12, Hp 452 etc etc (commas for example purposes only)

I have tried doing this by putting all db output into variables like this.

$AR = "AR" . "$row["AR"]" . "&nbsp;";

But I still have the bold AR showing whether there is a value or not. So it looks like this.

Item 1 AR 132

Item 2 AR 541

Item 3 AR

Item 4 AR

I only want the prefix to show if there is a value in the db column.

I hope these examples help.

As I said when I started this I am not good at explaining things especially when it comes to something I dont know all that much about.

Thank you to everyone that has and does help. I truly appreciate it.

ergophobe

8:47 pm on Jun 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Here is a quick and dirty hack job pasted from my other two posts. It only allows you to put the prefixed values in the LAST column of your table. This should get you started.

If you get this working and then want to figure out how to make it more flexible and robust, post back.

Tom

$prefix_array ('col2' => 'prefix2', 'col4' => 'prefix4');
$prefix_cols = array('col2', 'col4');

$query = "SELECT * FROM table1";
$result = mysql_query($query);

$table_rows ="";
while ($row = mysql_fetch_assoc($result))
{
$table_rows .= "<tr>";
$prefixed = "";

foreach ($row as $key => $val)
{

if (in_array($key, $prefix_cols) && $val)
{
$prefixed .= $prefix_array[$key] . $val . ", "; // leaves an extra comma + space which we will snip off later.
}
else
{
$table_rows .= "<td>$val</td>";
}

} // close foreach loop

// the substr() call snips off the last comma and space.
$table_rows .= "<td>". substr($prefixed, 0, -2) ."</td></tr>";

} close while loop

echo "<table>$table_rows</table>";