Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.
Sign In
Not a member yet? Click here to register.
Navigation

Quirky Coding Help Needed

Last updated on 1 month ago
GrimlochGrimloch
Posted 2 months ago
It's me again; the old shade-tree-programmer. I am in need of some more help with my blog infusion. Please; w/o criticism of my quirky coding habits and procedures.
Everything is working beautifully except my calendar module's article links. Actually that works correctly too as long as it is hard-coded into the script. This is the hard-code
that is an existing part of my calendar script:
$days = array(
        1=>array('../grims_blog/filtered.php?post_id=8','link'),
        7=>array('../grims_blog/filtered.php?post_id=9','link'),
        23=>array('../grims_blog/filtered.php?post_id=12','link'),
        24=>array('../grims_blog/filtered.php?post_id=13','link'),
);

This produces linked articles for the given DAY in the calendar correctly. What I need to do is make this array a dynamic one after querying my database table GRIMS_BLOG_POST_TOPIC.
where I need to get the art_day(article day) and the post_id. Then I need to build a string that is the same as the arrays in the above code. Something like this:

1=>array('../grims_blog/filtered.php?post_id=8','link'),
day + "=>array('../grims_blog/filtered.php?post_id=" + post_id + "','link'),"

Here is the code from my create_post script that records the post_id, topic_id, month and day correctly.
$result = dbquery("INSERT INTO ".DB_GRIMS_BLOG_POST." (post_id, topic_id, post_title, post_body, post_img, post_thb, posted, active) VALUES ('', '$topic_id', '$post_title', '$post_body', '$post_img', '$post_thb', '$posted.', '$active')");
$last_id = db_lastid();
$mymon = date("n", $posted);
$myday = date("j", $posted);
$result = dbquery("INSERT INTO ".DB_GRIMS_BLOG_POST_TOPIC." (post_topic_id, post_id, topic_id, art_mon, art_day) VALUES ('', '$last_id', '$topic_id', '$mymon', '$myday')");

I'm not sure how to go about doing the query and then building the links. If this makes sense to anyone I sure would appreciate any help with this.
Energy can neither be created nor destroyed; only transformed !
douwe_yntemadouwe_yntema
Posted 2 months ago
Maybe something like this (sorry adding as code is not working for me atm):

$result = dbquery ("Select art_day, post_id From DB_GRIMS_BLOG_POST_TOPIC Where .....")
if (dbrows($result)) {
$data = dbarray($result)

$na = array();

while ($data = dbarray($result)) {

$mdata = array();
$mdata['title'] = [code]'../grims_blog/filtered.php?post_id=".$data['post_id']."';
$mdata['link'] = 'link';

$na[] = $mdata;
}

}


You can check the contents of the array by adding statement var_dump($na) as test after the loop.



 [/code]
Edited by douwe_yntema on 01-12-2020 20:47, 2 months ago
GrimlochGrimloch
Posted 2 months ago
Thank you very much. I think it just might work. Can't check it now am at work but will check it out tonight.
GrimlochGrimloch
Posted 2 months ago
OK. This is the final query using your suggested code douwe. I pulled the info from November. In actuality the WHERE clause will have to be different and I'm not quite sure how to relate the current month to the value stored in art_mon(article month). That's another issue.

$result = dbquery("SELECT * From ".DB_GRIMS_BLOG_POST_TOPIC." WHERE art_mon='11'");
if (dbrows($result)) {
    $na = array();
        while($data = dbarray($result)) {
    $mdata = array();
        $dah = $data['art_day'];
        $pst = $data['post_id'];
    $mdata = $dah."=>array('../grims_blog/filtered.php?post_id=$pst','link'),";
    $na[] = $mdata;
    }
}
var_dump($na);

And this is the vardump result:

array(4) {
[0]=> string(58) "23=>array('../grims_blog/filtered.php?post_id=12','link'),"
[1]=> string(58) "24=>array('../grims_blog/filtered.php?post_id=13','link'),"
[2]=> string(56) "1=>array('../grims_blog/filtered.php?post_id=8','link'),"
[3]=> string(56) "7=>array('../grims_blog/filtered.php?post_id=9','link'),"
}

In the dump the 23 24 1 and 7 are the days in Nov the articles were written so that is correct and the post_id's at the end are correct so basically it works right. I'm assuming then that each line is one long string. So how do I get from that to this in the script?
[code]$days = array(
 1=>array('../grims_blog/filtered.php?post_id=8','link'),
 7=>array('../grims_blog/filtered.php?post_id=9','link'),
 23=>array('../grims_blog/filtered.php?post_id=12','link'),
 24=>array('../grims_blog/filtered.php?post_id=13','link'),
);
[/code]
douwe_yntemadouwe_yntema
Posted 2 months ago
With the last statement in de while loop, the array $na[] is appended with the content of the array $mdata[].

You need to append $mdata[] as an array, not as a string.
So line 9 needs to added square brackets and remove the $dah:
 $mdata[] = '../grims_blog/filtered.php?post_id=$pst','link');

and change the last line in the wile loop, where the array $na[] is filled as:
$na[$pst] = $mdata;
 
GrimlochGrimloch
Posted 1 month ago
Well I think my brilliant idea is just not going to work. No matter what I do and no matter how I configure this thing it won't work. The
[code]$days = array(

in the script screws everything up. When I try to include the "query coding" within that array it will not work. I really appreciate your attempts at helping me douwe. It does generate the right stuff I just can't incorporate it in the original script so I suppose I'm going to abandon my pursuit.[/code]
Edited by Grimloch on 02-12-2020 17:37, 1 month ago
douwe_yntemadouwe_yntema
Posted 1 month ago
Maybe you can do a var_dump($days) on the code underneath to see the difference between de original and the code from the new script:
$days = array(
 1=>array('../grims_blog/filtered.php?post_id=8','link'),
 7=>array('../grims_blog/filtered.php?post_id=9','link'),
 23=>array('../grims_blog/filtered.php?post_id=12','link'),
 24=>array('../grims_blog/filtered.php?post_id=13','link'),
);

i.m.o if the content of the array is the same, then it should work.
GrimlochGrimloch
Posted 1 month ago
When I do that in the calendar script itself this is what I get:
array(4) {
[1]=> array(2) { [0]=> string(36) "../grims_blog/filtered.php?post_id=8" [1]=> string(4) "link" }
[7]=> array(2) { [0]=> string(36) "../grims_blog/filtered.php?post_id=9" [1]=> string(4) "link" }
[23]=> array(2) { [0]=> string(37) "../grims_blog/filtered.php?post_id=12" [1]=> string(4) "link" }
[24]=> array(2) { [0]=> string(37) "../grims_blog/filtered.php?post_id=13" [1]=> string(4) "link" }
}

However I get this printed to the screen above the calendar proper for a VERY VERY LONG time: I assume for each day of the month:

 
Grimloch attached the following image:
cal2.jpg
douwe_yntemadouwe_yntema
Posted 1 month ago
This is the var_dump fromnthe hardcoded array?

Can you compare it with the array filled by the while loop?

 
GrimlochGrimloch
Posted 1 month ago
It's the same but with an added array for the "link". What it boils down to I think, is a 4 dimensional array. I have no experience with multi-dimensional arrays. Consequently I have absolutely no idea how to solve this.
 
GrimlochGrimloch
Posted 1 month ago
This is the most bizzare thing I have ever seen. I can generate the complete code but no matter where I put it in the calendar script it won't work. This is an image of my test file's output:
 
Grimloch attached the following image:
myblog.jpg
douwe_yntemadouwe_yntema
Posted 1 month ago
That is because if you set it manually like in your first post, the code generates an arry. Now you are outputting the code to generate the array. This is only a string. You should execute the generared code with eval, but this is not recommend.
ChanChan
Posted 1 month ago
Im sorry got busy with sorting out work for this site. I would like to post my share of input.

​I see that the column has day and month. To query multi column is slower than to query on 1 single column. I prefer the datestamp int(10) method. This would have been easier with this query to sort month and day using php. 

​​​​​​Insert to blog item using php timestamp. aka time(). To fetch the items would be easy with these:

// February 2020
WHERE MONTH(FROM_UNIXTIME(blog_timestamp)) = 2 AND YEAR(FROM_UNIXTIME(blog_timestamp)) = 2020

i would also add selector in SQL to give me the month and the year as well to sort into query output. 

SELECT * , MONTH(FROM_UNIXTIME(blog_timestamp)) as month, YEAR( FROM_UNIXTIME(blog_timestamp)) as year

So dbarray($res)) will have month and year in your dump. To sort into array I would do this:

$data['link'] = BASEDIR."....?id=$id";

$cache[ $year ][ $month ][ $id ] = $data;

​To callback items on your cached data.. 

Say month 2, Year 2020. will be

$feb =$cache[2020][2];

it will give u 2 dimension array you can loop like this

foreach ($feb as $id => $data) {
 print_p($data);
}

You can also use SQL to add selector DAY(FROM_UNIXTIME( datestamp )) as day, if you need it.

I did not include isset check or empty check in my code above. You can freely add them if needed.

That is how I would design dated stuffs with main priority in sortable and filter control at SQL level. PHP end is just mostly decorative adds and parser for best practise. I will not filter results using php.. proven very slow to render. 
GrimlochGrimloch
Posted 1 month ago
OK thanks Chan. I have a new problem that I need help with. I have changed my infusion table field from 'posted' int(10) TO 'post_date' date not null default='0000-00-00' and there is so much confusing information when I do google searches on date/time functions. With my column being only 'date' (no time needed or wanted), I cannot insert a record. I have tried many different scenarios to do this. How can I insert the current date in this field at post creation time? 'yyyy-mm-dd' format.

Sorry nevermind. I got it figured out. $post_date = date("Y-m-d");
Edited by Chan on 12-12-2020 02:32, 1 month ago
You can view all discussion threads in this forum.
You cannot start a new discussion thread in this forum.
You cannot reply in this discussion thread.
You cannot start on a poll in this forum.
You cannot upload attachments in this forum.
You can download attachments in this forum.
You cannot up or down-vote on the post in this discussion thread.
You cannot set up a bounty in this discussion thread.
Moderator: Support Team
Users who participated in discussion: Chan, Grimloch, douwe_yntema,