alphabetical array

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

Post Reply
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

alphabetical array

Post by aravona »

I've got a conditional statement which would draw from a database a single letter ( in this case, g or l )

When I've put this into my if statement, I'm going straight to my else, because my coding has decided my array wants to give 1 and not g as a readout.

I have always sucked at arrays and even with the help of the php manual I'm not able to get this to work o.o

Code: Select all

<?php
	$sql = "SELECT `cloth` FROM `ver_pro` where `pID` ='1' AND `prCode` = '" . $row->prCode . "'";
	$cloth = mysql_query($sql) or die(mysql_error());
	$cloth_data = mysql_fetch_array($cloth);
	if ($cloth_data['cloth'] == 'g'){
	require_once('g.php');
	}
	else {
	require_once('l.php');
	}
?>
eruna
Forum Newbie
Posts: 17
Joined: Mon Jun 28, 2010 2:02 pm

Re: alphabetical array

Post by eruna »

This should save you some work:

$file=$cloth_data['cloth'].".php";
require_once($file);

E
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

Re: alphabetical array

Post by aravona »

It would, if it actually fixed the problem I'm having in the first place lol... :(
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: alphabetical array

Post by pickle »

Output the query to the screen, then run the query via the command line - to see if the query actually is returning what you expect. If that doesn't show you the problem, call print_r() on $cloth_data to see if it is what you expect.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

Re: alphabetical array

Post by aravona »

I did 'echo print_r($cloth_data);' as suggested and I got the following: Array ( [0] => g [cloth] => g ) 1

And when I ran the sql through phpmyadmin, it came up with 'g' as well.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: alphabetical array

Post by pickle »

This is going to seem redundant, but call:

Code: Select all

echo '|'.$cloth_data['cloth'].'|';
To see if $cloth_data['cloth'] only contains a single character, which is "g"
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

Re: alphabetical array

Post by aravona »

nothings redundant if its eliminating the obvious :)

and yeah, I just got |g| which is what I expected >.<

Ahh found the problem ^^ each product has 4+ version >.< one of them was still blank, so the whole thing was getting its knickers in a twist, I got || on that page, put a g in, all working now :)
Post Reply