Jump to content

using array_slice()


VTS

Recommended Posts

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:

[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());
$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;
}
[/code]
Link to comment
Share on other sites

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:
[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];
}

[/code]
Link to comment
Share on other sites

try[code]
$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');[/code]
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

i make mistake
i echo field 'store' not 'store_id' in line [code]for ($x=0;$x<5;$x++) echo "store #: ".mysql_result($sql10,$x,'store');[/code]
corr to [code]for ($x=0;$x<5;$x++) echo "store #: ".mysql_result($sql10,$x,'store_id');[/code]
$x is number and you can do any statmens
Link to comment
Share on other sites

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!!
Link to comment
Share on other sites

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. 
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.