PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 3rd, 2000, 07:58 AM
xMonkey xMonkey is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2000
Posts: 16 xMonkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I apologize if this makes absolutely NO sense.
Ok I got a little problem, which I have a solution for but I would like to know if the is a better way.


I am making query to a mysql DB like so:

$query="Select * from DEMOTABLE";
$thisquery=mysql_query($query);
$demoinfo=mysql_fetch_array($thisquery);

Now I would like to make a table in HTML like
so:

<TABLE>
<?
while($demoinfo=mysql_fetch_array($thisquery);{
?>
<TR>
<?php
while($key=key($demoinfo)){
?>
<TD>
<?echo $demoinfo($key);?>
</TD>
<?
next($demoinfo);
}?>
</TR>
<?}?>

This does not work.

i get output as such.

column1 column1 column2 column2 column3 column3

becuase the keys for the $demoinfo array
are 'KEY0' 0 'KEY1' 1 'KEY2' 2

The keys are both the numeric and the alphenumeric. Doubling my output.

Now my stupid work around is to
add 2 next($demoinfo) commands. So I skip evry other column.

This works but there has to be another way.

Any help would be greate.

Thanks.


ps
the above code does not acutally prodce duoble output. becase it will stop here
while($key=key($demoinfo)){

on the second pass becuase the second pass
the key=0, the numeric key, which is false.

meaning I actually only get 1 column printed.

Reply With Quote
  #2  
Old March 3rd, 2000, 08:10 AM
KL KL is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2000
Posts: 2 KL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I think your problem lies in this row

while($key=key($demoinfo))

I think you should use

while (list($n, $val) = each($demoinfo))

Then the variable $val will contain the value you want to print and $n the number of the column.

Anyway try to look the php3 docs and search for the "each()" function.

=KL=

Reply With Quote
  #3  
Old March 3rd, 2000, 09:40 AM
xMonkey xMonkey is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2000
Posts: 16 xMonkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
OK I tried the suggestion. and the problem is not how I access the array.

the problem is this.

This Command

$demoinfo=mysql_fetch_array($thisquery);

The index keys for the $demoinfo are doubled.

Example.

The actual table in mysql is this

DEMO TABLE

| col1 | col2 | col3 | col4 |
+------+------+------+------|
| 1 | 10 | 109 | 45 |
|------+------+------+------+


so making the call:

$demoinfo=mysql_fetch_array($thisquery);

will return an array as such:

$demoinfo[0]==1
$demoinfo[1]==10
$demoinfo[2]==109
$demoinfo[3]==45

or you could access the same data with these keys

$demoinfo['col1']==1
$demoinfo['col2']==10
$demoinfo['col3']==109
$demoinfo['col4']==45

Using the numeric index or the aplha(words) index is the same.

The problem is that the actuall keys for the array have both.

using
while (list($n, $val) = each($userinfo)){
echo "$n $val <br>";
}

you get the output of such

col1 1
0 1
col2 10
1 10
col3 109
2 109
col4 45
3 45

the actual key data for the array is doubled
so if you make on call for the key and its value and increment the index pointer
and then make another call you get the SAME
DATA you are just accessing it with the
'col1' NAME the first time and then the '0' the numeric index the second.

Basicly if you use

mysql_fetch_array it creates an array in which the index has both the aplenumeric 'col1' and the numeric '0'


ANyway I think I'll look through the mysql section in PHP but I have several times but maybe I'm just over looking something.

Reply With Quote
  #4  
Old March 3rd, 2000, 09:53 AM
xMonkey xMonkey is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2000
Posts: 16 xMonkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
if the little table above is confusing I apologize here is a better version.
<br>

<pre>
DEMO TABLE

| col1 | col2 | col3 | col4 | +------+------+------+------|
| 1 | 10 | 109 | 45 | |------+------+------+------+

</pre>

I hope that turned out better.

Reply With Quote
  #5  
Old March 3rd, 2000, 09:57 AM
xMonkey xMonkey is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2000
Posts: 16 xMonkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
DOh!!!

OK forget the TABLE I think you can probably figure it out between the two poorly rendered ones.

Anyway thanks for any amount of patience you might be spending with me and my tables.


Reply With Quote
  #6  
Old March 3rd, 2000, 10:05 AM
xMonkey xMonkey is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2000
Posts: 16 xMonkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
OK I got it.

I once again RTFM and found that
mysql_fetch_array does create an index including the associative alphanumeric names
and the numeric indices for the columns.

However mysql_fetch_row will only return the numeric indices which solves my problem since I do not have to use the associative names anyway.

Thanks for the help.

And sorry for anyone who had to read through all that.

Thanks.


Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Handfulls with arrays.

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap