Jump to content


Photo

using array_slice()


  • Please log in to reply
12 replies to this topic

#1 VTS

VTS
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts
  • LocationTennessee

Posted 10 August 2006 - 09:53 PM

What I was trying to do was use array_slice to pick a store id and store it into a variable so I can display it.  When I used it like this, I got an array returned instead of a single value.  If you are wondering why I don't just use a while statement, it is because this is part of a page that is going to be displayed for one store at a time and is controlled by a submit button.  If you have problems understanding what I am trying to do just let me know and I will try to clarify.  By the way, run_sql() is a function I set up to use anytime I want to run a sql query if you were wondering.  Here is the code I am using:

 $sql = "SELECT COUNT(*) num
           FROM store
           WHERE store_open_closed = 'o'";
 $result = run_sql($sql) or die(mysql_error());
 $storesql = "SELECT store_id
                  FROM store
                  WHERE store_open_closed = 'o'
                  ORDER BY store_id";
 $sql10 = run_sql($storesql) or die(mysql_error());
 $sql11 = mysql_fetch_array($sql10);
 $stores = mysql_fetch_array($result);
 $x =0;
 if($x < $stores['num']){
    $storenbr = array_slice($sql11,$x,1);
    echo "store #: ".$storenbr;
 }


#2 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 10 August 2006 - 10:32 PM

array_slice retur array
try to print_r($storenbr);
you must use
echo "store #: ".$storenbr[0];


#3 VTS

VTS
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts
  • LocationTennessee

Posted 10 August 2006 - 10:47 PM

Ahhhh.  Ok thanks alot!!

#4 VTS

VTS
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts
  • LocationTennessee

Posted 11 August 2006 - 02:11 PM

Ok, I have changed that line and now it is not printing anything for it.  I also changed another line.  I got rid of the mysql_fetch_array() for $sql10 because I want to be able to choose the store number from the store table.  Can anyone tell me what I am doing wrong? 

Here is the new code:
$sql = "SELECT COUNT(*) num
          FROM store
          WHERE store_open_closed = 'o' ";
 $result = run_sql($sql) or die(mysql_error());
 $storesql = "SELECT store_id
                  FROM store
                  WHERE store_open_closed = 'o'
                  ORDER BY store_id";
 $sql10 = run_sql($storesql) or die(mysql_error());
 $stores = mysql_fetch_array($result);
 $x =0;
 if($x < $stores['num']){
    $storenbr = array_slice($sql10,$x,1);
    echo "store #: ".$storenbr[0];
 }



#5 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 11 August 2006 - 02:39 PM

try
$storesql = "SELECT store_id
                  FROM store
                  WHERE store_open_closed = 'o'
                  ORDER BY store_id";
$sql10 = run_sql($storesql) or die(mysql_error());
$num_of_stores=mysql_num_rows($sql10);
for ($x=0;$x<5;$x++) echo "store #: ".mysql_result($sql10,$x,'store');


#6 VTS

VTS
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts
  • LocationTennessee

Posted 11 August 2006 - 02:48 PM

Still didn't work.  I need it to work in an if statement so that I can put some more code in there to display an input page that will cycle one store at a time which is controlled by hitting a submit button.  When I use a for statement like that, it just displays it 5 times.  But it still didn't display the store number.  Thanks for the help though.  Any other ideas?

#7 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 11 August 2006 - 03:26 PM

i make mistake
i echo field 'store' not 'store_id' in line
for ($x=0;$x<5;$x++) echo "store #: ".mysql_result($sql10,$x,'store');
corr to
for ($x=0;$x<5;$x++) echo "store #: ".mysql_result($sql10,$x,'store_id');
$x is number and you can do any statmens


#8 VTS

VTS
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts
  • LocationTennessee

Posted 11 August 2006 - 03:45 PM

Well I tried that too and still no dice.  I really don't have any idea what to do with this.  The reason I didn't want to use the for statement is because I need to loop store by store one at a time and not just show them all on one page together.  When I use a for or while statment, I cannot get it to just show one store at a time.  Rather, it shows them all on the page.  My end goal with this code is to be able to have a page where a user is shown the store number and the fields that need to be input for that paticular store and when they hit the submit button, it stores the data in the datebase and then show the fields that need to be input for the next store and so on.  Thanks for the quick replies!!

#9 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 11 August 2006 - 04:04 PM

mysql_result($sql10,$x,'store_id') return from resorce $sql10 row: $x and field: 'store_id'
when $x=0 it returns 1st row etc.

#10 VTS

VTS
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts
  • LocationTennessee

Posted 11 August 2006 - 04:09 PM

I understand what that statement is saying.  What I meant by I don't know what to do with this is that I am stuck and don't know what to do since it did not work.  Thanks for all the help.  I really appreciate it. 

#11 VTS

VTS
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts
  • LocationTennessee

Posted 11 August 2006 - 04:57 PM

Anyone have any ideas?

#12 VTS

VTS
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts
  • LocationTennessee

Posted 11 August 2006 - 10:24 PM

I still haven't figured it out...hopefully someone knows what I am doing wrong here.

#13 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 11 August 2006 - 10:35 PM

you need pagination
one row per page
google for same tutorial




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users