jcbones
Staff Alumni-
Posts
2,653 -
Joined
-
Last visited
-
Days Won
8
Everything posted by jcbones
-
Yes, books is a multidimensional array. That means you have an array, nested inside of another array. You can have as many dimensions as you need, however, at this juncture you have 2 dimensions. So, your first foreach loop, goes through the first dimension, and looks at each key (starts at 0). This is why your numbers start as 0 - 2, however now your values are returning array, this is because the values are the second dimension of your array, or as some call it, nested. The second foreach loop, goes through the second dimension of your arrays, now we can access the keys (title, author, publisher) and the values. Since your array doesn't have any more dimensions, these values are not ARRAY's but STRINGS. You can echo out strings.
-
<?php $books = array( array("title"=>"Book Title 1", "author"=>"Author 1", "publisher"=>"Publisher 1"), array("title"=>"Book Title 2", "author"=>"Author 2", "publisher"=>"Publisher 2"), array("title"=>"Book Title 3", "author"=>"Author 3", "publisher"=>"Publisher 3"), ); foreach ($books as $key => $value) { foreach($value as $kk => $vv) { echo "$kk : $vv<br>"; } } ?>
-
Well, using the supplied code, I would. $number = 0; while($row = mysql_fetch_assoc($result)) { $storage_array[++$number] = $row['id']; } foreach($storage_array as $number => $questionID) { echo 'Question Number: ' . $number . ' is row ' . $questionID . ' in the database.<br />'; }
-
As Pik suggested: $number = 0; while($row = mysql_fetch_assoc($result)) { echo 'Number: ' . ++$number; }
-
PHP with Sql UPDATE command not working for numbers > 2
jcbones replied to gazapo's topic in PHP Coding Help
So, what is the database dump of your table structure? -
Pull it using javascript, will usually hide it (or at least create more steps to see it).
-
Of course, you don't need PHP for regex either. You can use it in MySQL too.
-
As suggested: //A switch switch(date('l')) { case 'Saturday': $time = strtotime('-1 day'); break; case 'Sunday': $time = strtotime('-2 day'); break; case 'Monday': $time = strtotime('-3 day'); break; case 'Tuesday': $time = strtotime('-4 day'); break; case 'Wednesday': $time = strtotime('-5 day'); break; case 'Thursday': $time = strtotime('-6 day'); break; default: $time = time(); } if(file_exists("services/2011/".date("ymd", $time)."Audio.mp3")) { echo "<a href='services/2011/".date("ymd", $time)."Audio.mp3'>Listen To Friday Sermon</a>"; } //or an Array: $days = array(-2,-3,-4,-5,-6,0, -1); if(file_exists('services/2011/' . date('ymd',strtotime($days[date('w')] . ' day')) . 'Audio.mp3')) { echo "<a href='services/2011/" . date('ymd',strtotime($days[date('w')] . ' day')) . "Audio.mp3'>Listen To Friday Sermon</a>"; } Of course, provided UN-TESTED! as usual.
-
You really should have DB date columns structured as "date" or "datetime" or "timestamp". This will solve these issues. There is a reason DB's have date functions built into them. So, If it were me, I would build a script, that would re-order the date, and insert it into a date field. Making my future interactions with the DB, painless.
-
I see nothing wrong with the OP's code, just a little more sanitation. Although, you may want to re-think how you are going about this. Such as: 1. Why do you need to have a form to create php code? You could just create/append/edit it in an editor, then ftp it to your site. 2. Why not use a database? Easier to maintain, and you wouldn't have ~infinity files.
-
Try this: Matches the clueid to the answerid, which would match funny to monkey, and boring to cat. SELECT * from clues, answers WHERE clues.clueid = answers.answerid AND clues.quizid = '{$_GET['quizid']}'
-
<div class="bag"><br/><?php $showCart = showCart(); echo writeShoppingCart(); ?> </div> <div id="contents"> <?php echo $showCart; ?> </div>
-
What does your database table look like, and how would this one quizid link to funny and boring? This may be due to database design, rather than the query design.
-
If you MUST do it the first way, change this line to: $parts = explode(' ',$value,2); It should fix that problem.
-
For something this simple, you could probably use: str_replace()
-
For grins, try: SELECT * from clues, answers WHERE clues.quizid = answers.quizid AND clues.quizid = '{$_GET['quizid']}'
-
Displaying master details page links in columns
jcbones replied to wchamber22's topic in PHP Coding Help
You must search your script, Dreamweaver is horrible about fetching the results before you actually need them, thereby moving the array_pointer one index (which makes it seem that you are missing a row). So, look for: mysql_fetch_assoc($rsBotanicalA) And delete it, (as long as it is above the code that Mj gave you above. Or, you can post your entire script here, inside the proper [ code ] blocks, and we will strip it out for you. -
is_file() doesn't just check to make sure it is a valid filename, it checks that the name given is an actual file. So therefore, checking the name in a database column will always return false, as a name in a database is NOT a file.
-
foreach($color as $value) { $parts = explode(' ',$value); echo 'Code: ' . $parts[0] . '<br />Color: ' . $parts[1]; }
-
Creating multidimension array from mysql source and outputting
jcbones replied to maxdean10's topic in PHP Coding Help
This is a project I worked on a couple years ago. It takes a multi-dim array from the database, and puts it into drop down boxes. I didn't go through your code, but figured it would either help you, or confuse you more. //call the query $result = mysql_query($query); //if we have data to pull, populate it. if(mysql_affected_rows() > 0) { while($row = mysql_fetch_array($result)) { $join_id = $row[0]; //make sure form name's don't have whitespace $join_event_id = $row[1]; $join_position = str_replace(' ','_',$row[2]); $join_candidate = $row[3]; //create a storage array ->Multi-dim. $storage[$join_event_id][$join_position][$join_candidate] = $join_id; } //cycle through our array to build our forms. foreach($storage as $id => $array2) { //lets start off by making a form. There should only be one intitial key, so we will build it on the first loop. echo '<form action="" method="post">' .'<input type="hidden" name="election" value="'.$id.'"/>'; //make a hidden input, this will hold the name of our election. echo 'Vote in Election: '. $id .'<br/>'; foreach($array2 as $position => $array3) { echo '<div>'.$position.'<br/>' //this holds the name of our position. .'<input type="hidden" name="position[]" value="'.$position.'"/>' //this hidden input holds the NAME of our next select input. .'<select name="'.$position.'">'; //notice that the name of this input is the same as the value of the input above. if(is_array($array3)) { foreach($array3 as $candidate => $canID) { echo '<option value="'.$canID.'">'.$candidate.'</option>'; //This is our options. } } else { echo '<option value="NULL">Not Set</option>'; //only invoked if there are no candidates (query screwed teh pooch). } echo '</select>' //ending our select box. .'</div>'; } echo '<br/><input type="submit" name="submit" value=" SUBMIT "/>'//submit the and end the form. .'</form>'; } } This is how I went about retrieving the values: //Check to see if the submit button has been hit if($_POST['submit']) { //escape our strings to protect from sql injection attacks -- //You could add more checks here $election = mysql_real_escape_string($_POST['election']); $position = $_POST['position']; //check to see if the user has already voted $checkDb = "Query removed."; $resultDb = mysql_query($checkDb); if(mysql_affected_rows() < 1) { foreach($position as $key => $value) { $position = mysql_real_escape_string($value); $id = mysql_real_escape_string($_POST[$value]); $position = str_replace('_',' ',$position); //if user hasn't voted, insert the query, and echo him a thank you. mysql_query("query removed. I would suggest not to query here, but build multi-values, and insert with 1 query."); } echo 'Thanks for voting!<br/>'; } //if the user has voted, tell him that. else echo 'You have already voted!<br/>'; } -
2 ways: 1. Use a form to ask the user for their timezone. 2. Have javascript set a cookie, send an ajax request to the server, check the cookie, then set the timezone with PHP. Only 2 ways I could think of, off the cuff.
-
Why not just include it. Otherwise, you will have to pass an HTTP request to file_get_contents(). This will only work if fopen wrappers are turned on in your php.ini.
-
problem connecting to mysql db using included connect file
jcbones replied to maxdean10's topic in PHP Coding Help
The reason for you situation is that you are using a HTTP address. This will call the file through HTTP, upon which the PHP parser will parse the file, and return the results. So your file will not see the connection, as it has already been opened and closed. You need to use a file path instead of HTTP. Pik's solution should work. -
display one thing first loop pass and something else second pass
jcbones replied to futrose's topic in PHP Coding Help
Show us your code, we'll show you how to fix that problem.